Blog > detail

Just my tiny mind

Javascript Delay or Pause or Sleep Loop

26-2-2012 Admin General 2 Comments

Wonder how to make Javascript Delay or Pause or Sleep Loop? In this post, I'll try to share what i found. This function of Javascript will delay or pause an existing loop for defined time.


Here is the code of Javascript Delay or Pause or Sleep Loop:


function loop(ceil,pause_every,pause_for,count){

if(!count)count=1;

if(count //execute your code here

if( (count%pause_every) == 0 && count != 0){

count++;

setTimeout("loop("+ceil+","+pause_every+","+pause_for+","+count+");",pause_for*1000);

}else{

count++;

loop(ceil,pause_every,pause_for,count);

}

}

}


Call the function like this:


loop(10,3,2);//the code will loop for 10 times and pause for 2 second each 3 times loop.


Hope this Javascript Delay or Pause or Sleep Loop code will be useful for you.

Javascript Delay or Pause or Sleep Loop

Javascript Delay or Pause or Sleep Loop

Javascript Delay or Pause or Sleep Loop

word word

Wonder how to make Javascript Delay or Pause or Sleep Loop? In this post, I'll try to share what i found. This function of Javascript will delay or pause an existing loop for defined time.


Here is the code of Javascript Delay or Pause or Sleep Loop:


function loop(ceil,pause_every,pause_for,count){

if(!count)count=1;

if(count //execute your code here

if( (count%pause_every) == 0 && count != 0){

count++;

setTimeout("loop("+ceil+","+pause_every+","+pause_for+","+count+");",pause_for*1000);

}else{

count++;

loop(ceil,pause_every,pause_for,count);

}

}

}



Javascript Delay or Pause or Sleep Loop

Javascript Delay or Pause or Sleep Loop

Javascript Delay or Pause or Sleep Loop