Jump to content

Countdown Timer Script Improvement


Goose87

Recommended Posts

Dear all,

 

I have the following script, but there is a problem that I am not sure how to resolve.  Someone looked at this before and mentioned that the reason it didn't display straight away was because the timer minuses one second, and then displays.  This looks very unprofessional. 

 

If someone could help me out and adjust the script to show the timer instantly as the page loads, I would be extremely grateful.  If you could mention which bit you adjusted too, I would appreciate it as I want to learn.

 

Thank you to all in advance,

 

James

 

<script type = "text/javascript">
var timeInSecs = null;
var ticker = null;

function startTimer(secs){
timeInSecs = parseInt(secs);
ticker = setInterval( "tick( )", 1000 ); 
}

function tick() {
var secs = timeInSecs;
if (secs > 0)    {
--timeInSecs; 
} 
else {
clearInterval( ticker ); 
}

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(<?php echo "$timer"; ?>);

</script>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.