HTML and CSS Reference
where timeID is a variable that stores the ID of the timed-interval command. To halt the
repeating command before it is next scheduled to be run, you use the clearInterval()
method with the timeID variable as follows:
clearInterval( timeID );
An important point to remember about the setTimeout() and setInterval() methods
is that after a browser processes a request to run a command at a later time, the browser
doesn't stop. Instead, the browser proceeds to any other commands running in the script
and processes those commands without delay. For example, you might try to run three
functions at 50-millisecond intervals using the following structure:
However, a browser would execute this code by running all three functions almost
simultaneously 50 milliseconds later. To run the functions with a separation of about
50 milliseconds between one function and the next, you would need to use three differ-
ent delay times, as follows:
In this case, a user's browser would run the first function after 50 milliseconds, the sec-
ond function 50 milliseconds after that, and the third function after another 50 millisec-
onds have passed.
Running Timed Commands
• To run a command after a delay, use the method
timeID = setTimeout(“ command ”, delay )
where command is the command to be run, delay is the delay time in millisec-
onds, and timeID is a variable that stores the ID associated with the time-delayed
• To repeat a command at set intervals, use the method
timeID = setInterval(“ command ”, interval )
where interval is the time, in milliseconds, between repetitions of the command.
• To cancel a specific time-delayed command, use the method
clearTimeout( timeID )
where timeID is the ID of the time-delayed command.
• To clear all time-delayed commands, use the following method:
• To cancel a repeated command, use the method
clearInterval( timeID )
where timeID is the ID of the repeated command.
• To clear all repeated commands, use the following method: