Search within TutorialsPark

Javascript BOM : Timers and Intervals


Learn to create Timers using the window Object.


Javascript Browser Object Model : Timer Methods

The JavaScript window object provides two methods which can be used to create timers and alarms

Even though JavaScript is executed by the browser in a single threaded, but it still allows users to schedule specific time intervals and points at which the code will execute.


Javascript Location Object: Properties

Property Description
setTimeout() To execute the script after a specific period of time.
setInterval() To automatically reschedule the execution of an expression at set intervals.
clearTimeout() To remove the pending timeout.
clearInterval() To remove the pending interval.

Example: Javascript BOM setTimeout()

Example: Javascript BOM setTimeout()

Give it a TRY! » Note:The function can take only two arguments.


Javascript Intervals and Timeout: clearTimeout()

The JavaScript method clearTimeout() is a complimentary to setTimeout(), it cancels any pending timeout.

Example: Javascript Intervals and Timeout clearTimeout() Method.

Give it a TRY! » Note: The variable myTimer is a unique ID used to cancel the timeout.


Javascript Timer and Intervals : method setInterval()

The setInterval() method functions similar to timeout but it automatically executes the code repeatedly after specific time intervals.

In the demo below, the box is made to move along the X axis by 5 px after each interval of 500ms(0.5 sec)

Example : Javascript Timer and Intervals : setInterval() Method

Give it a TRY! » Note: All intervals can be removed easily using the method clearInterval() .