Timers are used in JavaScript for things like animation, firing functions, etc. They run asynchronously so you don’t have to wait on them. Here is an example…
The setTimeout function fires code when we tell it to based upon the time we give it. In the above example, we are running a function that outputs a console log message after one (1) second. The time is the 1000 number. It is in milliseconds.
Notice that we are storing the function in a variable. This allows us to cancel the repeated code with the clearTimeout function as follows…
We can also set a function to run repeatedly with setInterval…
This will run until we tell it to stop. We do that with clearInterval…