November 9, 201015 yr Calling all javascripters!! I have a 24 hour javascript countdown that resets itself when it reaches 0. However when I refresh the browser it also resets the countdown. Does anyone know how I to set the countdown to keep going even if you refresh the page? Here's the code! Many thanks!
November 9, 201015 yr Calling all javascripters!! I have a 24 hour javascript countdown that resets itself when it reaches 0. However when I refresh the browser it also resets the countdown. Does anyone know how I to set the countdown to keep going even if you refresh the page? Here's the code! var timeInSecs; var ticker; function startTimer(secs) { timeInSecs = parseInt(secs); ticker = setInterval("tick()", 1000); } function tick( ) { var secs = timeInSecs; if (secs > 0) { timeInSecs--; } else { clearInterval(ticker); startTimer(86400); // start again } var hours= Math.floor(secs/3600); secs %= 3600; var mins = Math.floor(secs/60); secs %= 60; var pretty = ( (hours < 10 ) ? "0" : "" ) + hours + ":" + ( (mins < 10) ? "0" : "" ) + mins + ":" + ( (secs < 10) ? "0" : "" ) + secs; document.getElementById("countdown").innerHTML = pretty; } startTimer(86400); // 24 hours in seconds Many thanks! Can't you get it to use the server's time so when the page loads it starts from where it left off or is that not what you're after?
November 9, 201015 yr Author Basically, I need a countdown that lasts 24hours and re-starts at 12pm everyday. I don't want the browser refresh to affect it at all. I hope that helps!!
November 9, 201015 yr Basically, I need a countdown that lasts 24hours and re-starts at 12pm everyday. I don't want the browser refresh to affect it at all. I hope that helps!! I've had a few beers tonight so my brain isn't thinking clearily but I'd get the time vars from the server and using some AJAX magic call it back when the page loads.
November 9, 201015 yr Author I've had a few beers tonight so my brain isn't thinking clearily but I'd get the time vars from the server and using some AJAX magic call it back when the page loads. OoooooooK...and I how do I do that??
November 9, 201015 yr OoooooooK...and I how do I do that?? I can reply tomoz mate, in-depth... too much for my brain to handle tonight (beer and all) Quite simple stuff really though if you are an Advanced developer as your profile says
November 9, 201015 yr Author I can reply tomoz mate, in-depth... too much for my brain to handle tonight (beer and all) Quite simple stuff really though if you are an Advanced developer as your profile says Ha! I didn't know you did the rankings! Not quite a guru yet...!
November 9, 201015 yr Author Right, I've got the countdown working now but for some reason when the minutes or seconds reach single figures there isn't a leading zero. Here's what I mean: It currently displays: 12:7:8 (Hours Mins Secs) I want it to display: 12:08:04 (Hours Mins Secs) Thank you!
November 10, 201015 yr To get the current date in JavaScript, all you need is: var date = new Date(); var now = date.getDate(); Or, if you're looking to cut down on variables... var now = (new Date()).getDate(); This is the equivalent of PHP's $_SERVER['REQUEST_TIME'] and should save you having to learn AJAX. Just compare the current time to 12 noon and work out what your time should be from there. Your minutes (and seconds) can be easily checked and expanded to have a leading 0: var minutes = timeVariable.getMinutes() < 10 ? '0' + timeVariable.getMinutes() : timeVariable.getMinutes(); Basically, if the minutes are fewer than 10, display '0' and then the minutes, orhterwise just display the minutes
November 10, 201015 yr Author To get the current date in JavaScript, all you need is: var date = new Date();var now = date.getDate(); Or, if you're looking to cut down on variables... var now = (new Date()).getDate(); This is the equivalent of PHP's $_SERVER['REQUEST_TIME'] and should save you having to learn AJAX. Just compare the current time to 12 noon and work out what your time should be from there Hi Skateside, thank you for your reply. I'm happy with the timer now, however, all I need to know is how to add a zero to the seconds/minutes when they reach single figures.
November 10, 201015 yr I would have done the 0 business by using a pad function and implement it into the time. Different ways of doing stuff though I suppose!
Create an account or sign in to comment