/ Published in: JavaScript
Source: Creating time delays @ howtocreate
There are two ways of creating time delays with JavaScript. The first is more simple and will simply wait for a specified amount of time before executing a function. The second does the same but will repeatedly execute the function.
Note, most browsers have a minimum delay length of between 25 and 75 ms. If a shorter delay is specified, the actual delay will be the minimum delay length. Even with higher numbers, the delay is never perfect. Most browsers will take slightly longer than the time you ask for, typically just a few miliseconds error. Some may correct their errors over time with interval timers. Also note, setting many timers with short delays on one page will cause the browser to become slow and somewhat unresponsive. Three or four timers is usually the reliable limit.
There are two ways of creating time delays with JavaScript. The first is more simple and will simply wait for a specified amount of time before executing a function. The second does the same but will repeatedly execute the function.
Note, most browsers have a minimum delay length of between 25 and 75 ms. If a shorter delay is specified, the actual delay will be the minimum delay length. Even with higher numbers, the delay is never perfect. Most browsers will take slightly longer than the time you ask for, typically just a few miliseconds error. Some may correct their errors over time with interval timers. Also note, setting many timers with short delays on one page will cause the browser to become slow and somewhat unresponsive. Three or four timers is usually the reliable limit.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// ----------------------------- // setTimeOut window.setTimeout(referenceToFunction,timeInMilliseconds); window.setTimeout('runMoreCode()',timeInMilliseconds); window.setTimeout('runMoreCode(\''+someString+'\','+someNumber+')',10); window.setTimeout(function (a,b) { //do something with a and b },10,someString,someObject); // ----------------------------- // setTimeInterval window.setInterval(function (a,b) { //do something with a and b },10,someString,someObject); // ----------------------------- // Clearing timeouts and intervals var myInterval = window.setInterval(function (a,b) { myNumber++; },1000); window.setTimeout(function (a,b) { clearInterval(myInterval); },3500);
URL: http://www.howtocreate.co.uk/tutorials/javascript/timers