galvin Posted May 3, 2009 Share Posted May 3, 2009 I have this external JS file for a Countdown Timer which works fine.... // JavaScript Document var seconds = 0; var outputObj = false; function startTimer(startTime, timerOutputObj) { //Parse minutes and seconds into integers startTime = String(startTime); var mins = parseInt(startTime.split(':')[0]); if (isNaN(mins)) { mins = 0; } var secs = parseInt(startTime.split(':')[1]); if (isNaN(secs)) { secs = 0; } //Convert to seconds seconds = (mins * 60) + secs; outputObj = timerOutputObj; //Run the timer runTimer(outputObj); } function runTimer() { outputObj.value = formatTime(seconds); seconds--; if(seconds < 0) { window.alert("Time's up! Press OK to reveal missed answers."); // change timeout message as required // window.location = "yourpage.htm" // redirects to specified page once timer ends and ok button is pressed reveal();//does this go back to the } else { setTimeout('runTimer()', 1000); } } function formatTime(seconds) { var mins = Math.floor(seconds/60); if (mins<10) { mins = '0' + mins; } var secs = Math.round(seconds%60); if (secs<10) { secs = '0' + secs; } return (mins + ':' + secs); } This main function in this external file get called by ... function beginthegame(time) { startTimer(time, document.cd.disp); } The Timer starts fine, but there are certain instances where I need the Timer to stop. So, how do I STOP an existing running function? In layman's terms, how would I stop this Timer before it hit 00:00. I ask because certain code runs when the timer hits 00:00, and like I said, sometimes I don't want that code to run. I can hide the timer display, but I found that it still runs behind the scenes, so I need a way to completely STOP it. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/156712-solved-stop-a-function/ Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 When do you want to stop the timer? When it's 00:01, a second before the timer should end? Quote Link to comment https://forums.phpfreaks.com/topic/156712-solved-stop-a-function/#findComment-825202 Share on other sites More sharing options...
galvin Posted May 3, 2009 Author Share Posted May 3, 2009 No, for instance, people are entering answers on my site and they have a certain amount of time to do it. If time "runs out", certain code runs. But if someone enters all of the right answers BEFORE the time runs out, I run other code. In this "other" code, I'd like to add something that causes the timer function to stop running behind the scenes, otherwise unnecessary code will run. So the timer could be at anytime (00:10, or 00:44, etc, etc, etc) when I need it to stop running. I'm sure this isn't enough information and I apologize for that. Quote Link to comment https://forums.phpfreaks.com/topic/156712-solved-stop-a-function/#findComment-825209 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 Change your code to: // JavaScript Document var seconds = 0; var outputObj = false; var timeoutObj; function startTimer(startTime, timerOutputObj) { //Parse minutes and seconds into integers startTime = String(startTime); var mins = parseInt(startTime.split(':')[0]); if (isNaN(mins)) { mins = 0; } var secs = parseInt(startTime.split(':')[1]); if (isNaN(secs)) { secs = 0; } //Convert to seconds seconds = (mins * 60) + secs; outputObj = timerOutputObj; //Run the timer runTimer(outputObj); } function runTimer() { outputObj.value = formatTime(seconds); seconds--; if(seconds < 0) { window.alert("Time's up! Press OK to reveal missed answers."); // change timeout message as required // window.location = "yourpage.htm" // redirects to specified page once timer ends and ok button is pressed reveal();//does this go back to the } else { timeoutObj = setTimeout('runTimer()', 1000); } } function formatTime(seconds) { var mins = Math.floor(seconds/60); if (mins<10) { mins = '0' + mins; } var secs = Math.round(seconds%60); if (secs<10) { secs = '0' + secs; } return (mins + ':' + secs); } Then when you want to stop, if (timeoutObj) clearTimeout(timeoutObj); Quote Link to comment https://forums.phpfreaks.com/topic/156712-solved-stop-a-function/#findComment-825214 Share on other sites More sharing options...
galvin Posted May 3, 2009 Author Share Posted May 3, 2009 That appears to have done the trick, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/156712-solved-stop-a-function/#findComment-825220 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.