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
https://forums.phpfreaks.com/topic/165489-countdown-timer-script-improvement/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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