What happens is this:
1.) The countdown function is called
2.) A bunch of timeouts are set in a loop
3.) At the end of the loop,
i equals
num + 1
4.) The timeouts start firing. The current values of the variables
num and
i are still available to the timeout handler because of closures (you might want to look into that). Since the value if
i is
num + 1, the alert gives you
-1.
You obviously want the timeout handler to remember the value of i at the time the handler is defined. I recently wrote up a few ways to accomplish that in another thread:
http://www.codingforums.com/showthre...07#post1057805.
I'm sure you'll be able to adapt that to your situation.