Sesquipedalian Posted June 21, 2008 Share Posted June 21, 2008 Hey everyone, I'm new to Javascript and I'm not sure why my countdown isn't working... Please give any advice you have! <script> function MyTimer () { var first; var min; var sec; if (first !== 1) { sec = 0; min = 3; first = 1; } if (min > 0) { if (min == 1) { document.getElementById('minutes').innerHTML = min+' minute and'; } else { document.getElementById('minutes').innerHTML = min+' minutes and'; } } if (sec == 1) { document.getElementById('seconds').innerHTML = sec+' second'; } else { document.getElementById('seconds').innerHTML = sec+' seconds'; } if (sec === 0 && min > 0) { sec = 60; min--; } else { sec--; } setTimeout(MyTimer(),1000); } </script> You have <span id="minutes"></span> <span id="seconds"></span> left. <script> MyTimer();</script> Thanks, - Adam Quote Link to comment Share on other sites More sharing options...
xtopolis Posted June 22, 2008 Share Posted June 22, 2008 Error: too much recursion http://www.google.com/search?q=javascript+too+much+recursion&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a Google it Quote Link to comment Share on other sites More sharing options...
Sesquipedalian Posted June 23, 2008 Author Share Posted June 23, 2008 Thanks, but looking at the google results didn't really explain how my script is causing that? Could you explain? Thanks. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted June 23, 2008 Share Posted June 23, 2008 Actually I don't understand it either. I would try coding it a different way if you can think of one. Sorry I couldn't be much help. Quote Link to comment Share on other sites More sharing options...
Sesquipedalian Posted June 24, 2008 Author Share Posted June 24, 2008 That's alright. Thank you. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 24, 2008 Share Posted June 24, 2008 There are a few problems with that code. 1. The setTimeout function is called like this setTimeout(MyTimer, 1000); Note there are no brackets following the function name. 2. Your variables only have local scope. The min, sec & first variables are renewed each time the function runs. So, even if you fixed item #1 you would see 3 minutes constantly because each call to the function would have it thinking it was the first time run. 3. When reducing the seconds at the end of a minute you need to go to 59 seconds not 60. (Ex: if you have 2 minues and 0 seconds and take away one second you have 1 minute and 59 seconds, not 1 minute and 60 seconds) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.