Timing Functions
Many built in javascript functions take callback functions. For example, setInterval
and setTimeout
both take a function and run it at a specific time.
setTimeout & setInterval
setTimeout
takes two arguments:
the function to be run
how long to wait (in milliseconds) before running that function
setInterval
also takes two arguments:
the function to be run
how often to run the function (in milliseconds)
You may want to have multiple instances of these timing events going, so you can differentiate between the instances by assigning the functions to variables.
You can disable an interval using clearInterval
:
And you can disable your setTimeout
before the function fires using clearTimeout
:
Exercises:
1. Use setInteral
and setTimeout
to write a program that prints the following:
(This should mimic a countdown, so each line will print after a one second delay.)
2. How could you mimic the setInterval
functionality using setTimeout
? Use setTimeout
to recreate the var kids = setInterval(annoy, 1000);
functionality.
Last updated