Run setInterval a few times, then pause for a few seconds

I need my code to run x a number of times, and then pause 30 seconds or so before resuming. Any ideas?

myslidefunction(); var tid = setInterval(myslidefunction, 1000); function myslidefunction() { setTimeout(function () { //do stuff }, 400); }; 
+4
source share
2 answers

You can save the execution counter and use normal_duration + 30000 as a setTimeout delay for X + 1st time.

 var runCount = 0, runsBeforeDelay = 20; function myslidefunction(){ // .. stuff runCount++; var delay = 0; if(runCount > runsBeforeDelay) { runCount = 0; delay = 30000; } setTimeout(myslidefunction, 400 + delay); }; // start it off setTimeout(myslidefunction, 1000); 
+8
source
 var counter = 0; var mySlideFunction = function(){ /* your "do stuff" code here */ counter++; if(counter>=10){ counter = 0; setTimeout(mySlideFunction, 30000); }else{ setTimeout(mySlideFunction, 1000); } } mySlideFunction(); 
+1
source

Source: https://habr.com/ru/post/1484454/


All Articles