123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|702|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> Web Development -> Javascript countdown timer ? :(

Sun, 28 Oct 2012, 07:05
spinal
I'm needing a little help getting multiple countdown timers on my page. can anyone explain to me why the timers dont update?



-=-=-
Check out my excellent homepage!
Sun, 28 Oct 2012, 07:38
CodersRule
on line 17 should be
so you don't need to use that ugly conditional.

Your actual problem is on line 35:

You're not actually concatenating the container, so it's trying to execute countdown with "container" as the element ID. You need to do this, instead:


This is causing other problems, though. All of the timers get reset to the third timer on update. I'm still debugging that bit.

I debugged the setTimeout() part by looking at my browser's javascript console with Ctrl+Shift+J. It told me that you were trying to access the innerHTML property of null, which meant that the getElementById(container) was not returning a valid element. Then I looked at all the places where the container variable is set, and figured out where the problem was.

edit: figured out the bit that had gone wrong.
theyear, themonth, and theday were initialised as global variables. This meant that whichever timeout that ran last would essentially "take over" those variables, setting them in all the other timeouts too. The setTimeout method calls the function from a global area of the scope, and in essence it's just concatenating (in your case),

to the end of the code. That means that if the variables themselves aren't global, the timeout will not work.

The best way to make the timeout work (a way that is also a lot less confusing, because you don't have to escape anything and it's not a disguised eval()) is to pass an anonymous function as the first parameter. That will run the function inside of the current scope, and make use of local variables:



The final, working code is this:


I should point out that it will show seconds as a single digit if the number is only one digit long, which probably isn't what you want (it'll show 787:15:4:5, for example). I'm not going to tell you how to fix that, though, because I need to go get some food now.

I might also point out that it's not a good idea (slow, inefficient) to initialise Date objects by concatenating numbers into a string and using Date.parse. Look at the Date() constructors on this page to see how else you might initialise Date()s.
Sun, 28 Oct 2012, 07:59
spinal
Wow, excellent, thanks Tim! I hope you enjoy your food, you deserve it

-=-=-
Check out my excellent homepage!