Click to See Complete Forum and Search --> : clock


stevecop
10-27-2003, 08:29 PM
Hi, is there a way for me to set my own time and then starting from the time I specify, to start live counting for the code below?


function clock()
{
var now = new Date();

var hours = String(now.getHours());
var minutes = String(now.getMinutes());
var minutes = String(now.getSeconds());

document.getElementById('clock').innerHTML = hours+':'+minutes+':'+seconds;
setTimeout("clock()", 1000);
}


Thanks in advanced.

agminer
10-27-2003, 11:36 PM
Saw a count down timer(s) at www.cokemusic.com
looking at the frame source I copied the following code. I don't know how they set there start time but here it is. Good luck deciphering it!!!

var hr=new Array();
var mn=new Array();
var sec=new Array();
var clockID=0;
var theTimeVarName=new Array();
var infmin=0;
var supmax=0;

function initTimer(hour,min,second,elemnum) {
hr[elemnum] = hour;
mn[elemnum] = min;
sec[elemnum] = second;
theTimeVarName[elemnum] = "theTime" + elemnum;
if(hr[elemnum]==0 && mn[elemnum]==0 && sec[elemnum]==0) {
document.paginateTicketForm[theTimeVarName[elemnum]].value="00:00:00";
}
supmax = Math.max(supmax,elemnum);
}

function refreshTimer(){
for (j=infmin;j<=supmax;j++) {
if (document.paginateTicketForm[theTimeVarName[j]] != null)
updateTimer(j);
}
clockID = setTimeout("refreshTimer()", 1000);
}



function updateTimer(cpt){
if(clockID[cpt])
{

clearTimeout(clockID[cpt]);
clockID[cpt] = 0;
}

if(hr[cpt]<=0 && mn[cpt]<=0 && sec[cpt] <= 0) {
document.paginateTicketForm[theTimeVarName[cpt]].value="closed";
}
else {
sec[cpt] = sec[cpt]-1;
if(sec[cpt]<=0 && mn[cpt]<=0 && hr[cpt]<=0) {
document.paginateTicketForm[theTimeVarName[cpt]].value="closed";
}else if(sec[cpt]<0) {
sec[cpt]=59;
mn[cpt] = mn[cpt]-1;
if(mn[cpt]<0) {
mn[cpt]=59;
hr[cpt]=hr[cpt]-1;
if(hr[cpt]<0)
hr[cpt]=0;
}
}
}

sec[cpt]=sec[cpt]+"";
if(sec[cpt].length==1) {
sec[cpt]="0"+sec[cpt];
}
mn[cpt]=mn[cpt]+"";
if(mn[cpt].length==1){
mn[cpt]="0"+mn[cpt];
}
hr[cpt]=hr[cpt]+"";
if(hr[cpt].length==1) {
hr[cpt]="0"+hr[cpt];
}

if(hr[cpt]<=0 && mn[cpt]<=0 && sec[cpt] <= 0) {
document.paginateTicketForm[theTimeVarName[cpt]].value="closed";
}
else
document.paginateTicketForm[theTimeVarName[cpt]].value=""+hr[cpt]+":"+mn[cpt]+":"+sec[cpt];
}

Charles
10-28-2003, 05:34 AM
<script type="text/javascript">
<!--
Date.ONE_SECOND = 1000;
Date.ONE_MINUTE = Date.ONE_SECOND * 60;
Date.ONE_HOUR = Date.ONE_MINUTE * 60;
Date.ONE_DAY = Date.ONE_HOUR * 24;
Date.ONE_WEEK = Date.ONE_DAY * 7;

function TimeUntil (d) {this.time = d.getTime ? d.getTime() : Date.parse(d)}

TimeUntil.prototype.valueOf = function () {return this.time - new Date ().getTime()}

TimeUntil.prototype.toString = function () {
var t = Math.abs(this.valueOf());
var d = Math.floor (t / Date.ONE_DAY);
var h = Math.floor ((t % Date.ONE_DAY) / Date.ONE_HOUR);
var m = Math.floor ((t % Date.ONE_HOUR) / Date.ONE_MINUTE);
var s = Math.floor ((t % Date.ONE_MINUTE) / Date.ONE_SECOND);
return (this.valueOf () < 0 ? '-' : '') + [d, d == 1 ? 'day' : 'days', [h, m < 10 ? '0' + m : m, s < 10 ? '0' + s : s].join(':')].join(' ');
}

var time = new TimeUntil ('1 January 2004');

document.write('<p id="time">',time > 0 ? time : 'The time has come.', '</p>');

if (document.getElementById) setInterval ("document.getElementById('time').firstChild.data = time > 0 ? time : 'The time has come.'",0.2 * Date.ONE_SECOND);

// -->
</script>