View Full Version : Help: Looking for javascript countdown script
Compnerd
24-02-2007, 22:09
Hi everyone, I am looking for a countdown script using javascript. If you have one, please let me know!
Uberbots
24-02-2007, 22:21
This is a simple function that i use on our website.
function update(divID, seconds) {
obj = document.getElementById(divID);
var now = new Date();
var output = "";
var days, hours, minutes, second;
var timeUntil = seconds - Math.round(now.valueOf()/1000);
days = Math.floor(timeUntil/86400); timeUntil -= days*86400;
hours = Math.floor(timeUntil/3600); timeUntil -= hours*3600;
minutes = Math.floor(timeUntil/60); timeUntil -= minutes*60;
second = Math.floor(timeUntil);
output += days + " Days, " + hours + " Hours, " + minutes + " Minutes, " + second + " Seconds";
obj.innerHTML = output;
window.setTimeout("update('" + divID + "', " + seconds + ")", 1000);
}
You supply the ID of the tag you want to fill with the countdown, and the UNIX timestamp of the event that will be occurring. make sure that you set a preliminary timeout function before using it the first time. for example:
window.setTimeout("update('feild0', 1173963600)", 0);
just for clarification, despite the tag, it is javascript.
also Dynamic Drive has tins of cool javascript samples. here is one that does a countdown. http://www.dynamicdrive.com/dynamicindex6/universalcountdown.htm
/forest
pheadxdll
24-02-2007, 23:44
Any of the above scripts are acceptable. Here's ours:
dateFuture = new Date(2007,2,28,24,0,0); // Set the date
function GetCount(){
dateNow = new Date(); //grab current date
amount = dateFuture.getTime() - dateNow.getTime(); //calc milliseconds between dates
delete dateNow;
// time is already past
if(amount < 0){
document.getElementById('countbox').innerHTML="Now!";
}
// date is still good
else{
days=0;hours=0;mins=0;secs=0;out="";
amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs
days=Math.floor(amount/86400);//days
amount=amount%86400;
hours=Math.floor(amount/3600);//hours
amount=amount%3600;
mins=Math.floor(amount/60);//minutes
amount=amount%60;
secs=Math.floor(amount);//seconds
if(days != 0){out += days +" day"+((days!=1)?"s":"")+", ";}
if(days != 0 || hours != 0){out += hours +" hour"+((hours!=1)?"s":"")+", ";}
if(days != 0 || hours != 0 || mins != 0){out += mins +" minute"+((mins!=1)?"s":"")+", ";}
out += secs +" seconds";
document.getElementById('countbox').innerHTML=out + " until we attend the Palmetto Regional in SC!";
setTimeout("GetCount()", 1000);
}
}
window.onload=function(){
GetCount();
}
Make a div with an id countbox where you want the timer to be. Done!
Note: Its set currently to the Palmetto Regional, change the date to whatever you want :P
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.