Jump to content

[SOLVED] Can somone show me how its done?


Dethman

Recommended Posts

Hey guys I am new to Javascript and I need to know how to make an actually count down script that counts down the seconds without having to refresh the page its kinda like this

 

Housing Due Date: 5hrs 10mins 22seconds

 

21

20

19

 

Anyone get what I mean also can you show me how to use it so I can manipulate it to whatever I want as for the time wise....

 

Thank you all for your time,

 

Brian Flores CEO,

Nimbusgames,llc

Link to comment
https://forums.phpfreaks.com/topic/115564-solved-can-somone-show-me-how-its-done/
Share on other sites

I wrote this like 2 years ago, but it came to mind.  It's not the best script in the world, but maybe it'll give you something to base your script on.

 

function GetObj(name) {
if (document.getElementById) {
	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
}
else if (document.all) {
	this.obj = document.all[name];
	this.style = document.all[name].style;
}
else if (document.layers) {
	this.obj = document.layers[name];
	this.style = document.layers[name];
}
}


function CountDown(seconds, div, url) {

if(seconds <= 0) {
	ChangeHTML(div, 'Updates in 0 seconds');
	window.location = url;
}
else {
	var seconds_next = seconds - 1;
	var ftime = FormatTime(seconds);
	var display = new Array();
	display['d'] = 'days';
	display['h'] = 'hours';
	display['m'] = 'minutes';
	display['s'] = 'seconds';
	if(ftime['days'] == 1) display['d'] = 'day';
	if(ftime['hours'] == 1) display['h'] = 'hour';
	if(ftime['minutes'] == 1) display['m'] = 'minute';
	if(ftime['seconds'] == 1) display['s'] = 'second';
	if(ftime['days'] > 0) {
		var str = 'Updates in '+ftime['days']+' '+display['d']+', '+ftime['hours']+' '+display['h']+', '+ftime['minutes']+' '+display['m']+' and '+ftime['seconds']+' '+display['s']+'.';
	}
	else if(ftime['hours'] > 0) {
		var str = 'Updates in '+ftime['hours']+' '+display['h']+', '+ftime['minutes']+' '+display['m']+' and '+ftime['seconds']+' '+display['s']+'.';
	}
	else if(ftime['minutes'] > 0) {
		var str = 'Updates in '+ftime['minutes']+' '+display['m']+' and '+ftime['seconds']+' '+display['s']+'.';
	}
	else {
		var str = 'Updates in '+ftime['seconds']+' '+display['s']+'.';
	}
	ChangeHTML(div, str);
	setTimeout('CountDown('+seconds_next+', \''+div+'\', \''+url+'\')', 1000);
}
}

function ChangeHTML(elem, text) {
elem = new GetObj(elem);
elem.obj.innerHTML = text;
}

function FormatTime(seconds) {
var ret = new Array();
ret['days'] = 0;
ret['hours'] = 0;
ret['minutes'] = 0;
ret['seconds'] = 0;
minute = 60;
hour = 3600;
day = 86400;
if(seconds >= day) {
	ret['days'] = Math.floor(seconds/day);
	seconds = seconds - (ret['days']*day);
}
if(seconds >= hour) {
	ret['hours'] = Math.floor(seconds/hour);
	seconds = seconds - (ret['hours']*hour);
}
if(seconds > minute) {
	ret['minutes'] = Math.floor(seconds/minute);
	seconds = seconds - (ret['minutes']*minute);
}
ret['seconds'] = seconds;
return ret;
}

 

Example usage:

<div id="countdown">There are 3 hours and 4 minutes left.</div>
<script language="javascript">CountDown('11040', 'countdown', '');</script>

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.