ITSE 1411 Beginning Web
JS Timeouts and Intervals Chapter 4 Discussion
Discussion
- The window object's timeout and interval method is used to create code that executes automatically.
- The setTimeout() method is used to execute code after a specific amount of time has elapsed. This method
is only called once. The syntax for the setTimeout() method is:
setTimeout("code to be executed", milliseconds of time to wait until code executes);
- The clearTimeout() method is used to cancel a setTimeout() method before the code executes. For example:
var buttonNotPressed = setTimeout("window.alert('Press ok to continue')", 10000);
clearTimeout(buttonNotPressed);
When the buttonNotPressed is true the setTimeout() is called. When buttonNotPressed is false the setTimeout() is cancelled.
- The setInterval() method is used to execute code except that it is called repeatedly after being called once. The
syntax for the setInterval() method is the same as the setTimeout() method.
- The clearInterval() method is used to cancel a setInterval() method and uses the same syntax as the clearTimeout() method.