View Full Version : countdown timer
izlik
Dec 11th 2007, 1:54 pm
I wonder if someone knows where i can get a countdown script for 24hours, or how one can be made? with active javascript
basiclly what i need is, when 24hours of the set time has run, it resets to 0 and begins to count down from 24hours again... like if i put it on 09:00PM it should start counting down from there and reset when it reach 09:00Pm again the next day and start over...
hope someone can help me.
hrcerqueira
Dec 12th 2007, 6:36 am
Are you counting on having a page showing during the 24 hours?
You can always use setInterval, or setTimeout.
Imagine that you have a span element like this:
<span id="timer"></span>
a quick scratch, counting only hours, can look something like this:
var timer = document.getElementById('timer');
function startTimer() {
window.clearInterval(interval);
var currTime = new Date().getTime();
var finishTime = new Date(currTime + 24 * 3600 * 1000).getTime();
var interval = window.setInterval(function() {
currTime = new Date().getTime();
var dif = (finishTime - currTime) / 1000;
if (dif <= 0) {
startTimer();
return;
}
var hours = parseInt(dif / 3600);
var minutes = parseInt((dif % 3600) / 60);
var seconds = parseInt((dif % 3600) % 60);
document.body.innerHTML = hours + ':' + minutes + ':' + seconds;
}, 500);
}
window.onload = startTimer;
temp2
Dec 13th 2007, 8:26 am
good tut, thank you very much
-----------
setTimeout:
Evaluates an expression after a specified number of milliseconds has elapsed
In versions earlier than Microsoft® Internet Explorer 5, the first argument of setTimeout must be a string. Evaluation of the string is deferred until the specified interval elapses.
As of Internet Explorer 5, the first argument of setTimeout can be a string or a function pointer.
The specified expression or function is evaluated once. For repeated evaluation, use the setInterval method.
When you use the setTimeout method with Introduction to DHTML Behaviors , the value of vCode should be a function pointer to call a function within the HTML Component (HTC) file or a string to call a function in the primary document.
setInterval Method
Evaluates an expression each time a specified number of milliseconds has elapsed.
The setInterval method continuously evaluates the specified expression until the timer is removed with the clearInterval method.
In versions earlier than Microsoft® Internet Explorer 5, the first argument of setInterval must be a string. Evaluation of the string is deferred until the specified interval elapses.
As of Internet Explorer 5, the first argument of setInterval can be passed as a string or as a function pointer.
To pass a function as a string, be sure to suffix the function name with parentheses. window. setInterval ("someFunction()", 5000);
When passing a function pointer, do not include the parentheses. window. setInterval (someFunction, 5000);
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.