setTimout() Test
Starts:
Stops:
Description
When I was teaching the setTimeout() function to my Spring 2005 CHum 284 class, I was explaining the importance of always making sure you clear your timeout before starting another one from the same button call to make sure more than one timer wasn't running for the same event. I explained that a single variable was needed to do this and that it held the reference number of the timer running. It could then be cleared to prevent more than one setTimeout running.
One of my students asked how the single variable could keep track of all the timers running. I made this file to help explain what was going on. To see it yourself, push the start button once. Things work normally and the event is only running once. However, if you push the start button lots of times you can see that lots of different events are running at the same time.
The number outputted on the screen is the reference number in the single variable to the current running timer. Each time you push stop, it kills the timer with that reference number. You can see that the reference number is always changing because a new timer is always being created. When you push stop, it doesn't stop the timers in the order that they were created; it stops the last timer made.
The single variable isn't keeping track of all the timers. It only knows the reference number for the newest timer. Because of the recursion taking place in this script, pushing the stop button will eventually cancel out all the timers running.