How to start and stop/pause setInterval?

后端 未结 6 837
野性不改
野性不改 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:36

    (function(){
        var i = 0;
        function stop(){
            clearTimeout(i);
        }
    
        function start(){
            i = setTimeout( timed, 1000 );
        }
    
        function timed(){
           document.getElementById("input").value++;
           start();
        }
    
        window.stop = stop;
        window.start = start;
    })()
    

    http://jsfiddle.net/TE3Z2/

提交回复
热议问题