
stuart |
There are three main functions for handling intervals in JavaScript; setInterval, clearInterval and setTimeout.
If you want something to happen every second for example you could do this:
setInterval(function() {
// Add your code here
}, 1000);
The 1000 is how many milliseconds it should wait before running again. 1000 milliseconds => 1 second.
If you want to stop a setInterval from running again then you can do this:
// Run 5 times then stop
var i = 0,
timer = setInterval(function() {
document.write('Run ' + i + ' times');
if( i == 5 ) {
window.clearInterval(timer);
}
}, 1000);
The last function is if you want to run a function in a second for example but only once.
// Run this function in 5 seconds
setTimeout(function() {
// Your Code
}, 5000);
Hope this helps!
Thursday, 17th December 2009
| Votes: |
|
16 |
|
9 |
|