Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Javascript Countdown Query

Featured Replies

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! smile.gif

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! smile.gif

 

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?

  • 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!! pardon.gif

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!! pardon.gif

 

 

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.

  • 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?? huh.gif

 

 

 

OoooooooK...and I how do I do that?? huh.gif

 

 

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 ;)

  • 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...! tongue.gif

 

  • 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! :D

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

  • 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.

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.