Welcome!

Join our community of MMORPG enthusiasts and private server developers! By registering, you'll gain access to in-depth discussions on source codes, binaries, and the latest developments in MMORPG server files. Collaborate with like-minded individuals, explore tutorials, and share insights on building and optimizing private servers. Join us today and unlock the full potential of MMORPG server development!

Join Today!

[Request]Countdown timer?

no bueno.
Joined
Jul 29, 2009
Messages
527
Reaction score
205
Location
Michigan
Can someone make me a simple countdown timer?

It has to repeat.
Like, my server restarts at midnight EST time.
So I need something to countdown to that, then restart as soon as it hits midnight.

I'm to lazy to do it (:
 
PHP:
<script type = "text/javascript">

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

</script>

<span id="countdown" style="font-weight: bold;"></span>
 
1. Avoid eval at all costs. setInterval will take a function, passing a string invokes eval.
2. Never assume the timing on setInterval or setTimeout is reliable based on the time you give it (ie, passing 1000 may result in ticks that are anywhere from 1ms to 50000000+ ms apart).
3. You don't need to parseInt an integer, and in the event that you are using parseInt, you should indicate the base.
4. This does not take timezone differences into account, whenever you are rendering a time or a timer on a user's browser, it should reliably show the correct time.
 
Off topic - but I had to point it out:

SELyA - [Request]Countdown timer? - RaGEZONE Forums

^Unintended reverse psychology.
 
PHP:
<script type = "text/javascript">

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

</script>

<span id="countdown" style="font-weight: bold;"></span>

I believe the guy who actually made it would have liked if you included the link where you found it.

(tha link ya know): Need A Repeating Countdown Timer [Archive] - CodingForums.com [POST FROM Philip M, Post number 2]
 
Back