Changing the interval of SetInterval while it's running

后端 未结 16 1377
南旧
南旧 2020-11-22 07:35

I have written a javascript function that uses setInterval to manipulate a string every tenth of a second for a certain number of iterations.

function timer(         


        
16条回答
  •  忘掉有多难
    2020-11-22 08:07

    I had the same question as the original poster, did this as a solution. Not sure how efficient this is ....

    interval = 5000; // initial condition
    var run = setInterval(request , interval); // start setInterval as "run"
    
        function request() { 
    
            console.log(interval); // firebug or chrome log
            clearInterval(run); // stop the setInterval()
    
             // dynamically change the run interval
            if(interval>200 ){
              interval = interval*.8;
            }else{
              interval = interval*1.2;
            }
    
            run = setInterval(request, interval); // start the setInterval()
    
        }
    

提交回复
热议问题