Skip to content Skip to sidebar Skip to footer

How To Make My Script Pause After Reaching A Certain Condition For 1000ms

I've got a setInterval script that repeats logging 'Hello world' 10 times. I would like to make it stop for 1 second after repeating 10 times, then starting again and doing the pro

Solution 1:

So you need to use clearInterval and use the id that is returned from setInterval to clear it.

functionmyTimer() {
    var i = 0,
    interval = setInterval(function(){
        console.log("Hello world");
        i++;
        if(i >=10){
            clearInterval(interval);
            window.setTimeout(myTimer, 1000);
        }
    },100);
}

https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Timers

Solution 2:

just with setTimeout and Recursion DEMO

var i = 1;
var delay = 500;

functioncallMe() {
  console.log("Hello World"+i);
  document.getElementById("pri").innerHTML = "Hello World " + i;

  if (i == 11) {
    i = 1;
    delay = 1000;
    console.log("......Restart");
    document.getElementById("pri").innerHTML = "......Restart";
  } else {
    delay = 300;
  }
  
  setTimeout(callMe, delay);
  ++i;
}

window.onload = callMe;
<divid="pri"></div>

Post a Comment for "How To Make My Script Pause After Reaching A Certain Condition For 1000ms"