How to start and stop/pause setInterval?

后端 未结 6 836
野性不改
野性不改 2020-12-05 02:59

I\'m trying to pause and then play a setInterval loop.

After I have stopped the loop, the \"start\" button in my attempt doesn\'t seem to work :

6条回答
  •  时光说笑
    2020-12-05 03:37

    See Working Demo on jsFiddle: http://jsfiddle.net/qHL8Z/3/

    $(function() {
      var timer = null,
        interval = 1000,
        value = 0;
    
      $("#start").click(function() {
        if (timer !== null) return;
        timer = setInterval(function() {
          $("#input").val(++value);
        }, interval);
      });
    
      $("#stop").click(function() {
        clearInterval(timer);
        timer = null
      });
    });
    
    
    
    

提交回复
热议问题