Original (code that works):
// v1.0
var clockElement = document.getElementById("Clock");
var clockZone = "(CST)";
var clockHours = 2;
var clockMinutes = 8;
var clockSeconds = 21;
var clockMilleseconds = 1000 - 321;
function clockDisplay() {
if (clockSeconds > 59) {
clockSeconds = 0;
if (++clockMinutes > 59) {
clockMinutes = 0;
clockHours = (clockHours + 1) % 24;
}
}
var hours = clockHours;
var ampm = (hours >= 12) ? " PM " : " AM ";
if (hours > 12) { hours -= 12; }
else if (hours == 0) { hours = 12; }
clockString = (hours < 10 ? "0" : "") + hours + ":" + (clockMinutes < 10 ? "0" : "") + clockMinutes + /*":" + (clockSeconds < 10 ? "0" : "") + clockSeconds + */ampm + clockZone;
clockElement.innerHTML = clockString;
++clockSeconds;
}
function clockUpdate() {
clockDisplay();
setInterval(clockDisplay, 1000);
}
clockDisplay();
setTimeout(clockUpdate, clockMilleseconds);
New (code that doesn't work):
function Clock(elementID, TimeZone, hour, minutes, second, millesecond) {
this.clockElement = document.getElementById(elementID);
this.clockZone = "(" + TimeZone + ")";
this.clockHours = hour;
this.clockSeconds = second;
this.clockMilleseconds = 1000 - millesecond;
this.firstIntervalReached = false;
this.update = false;
this.display = function() {
if (this.clockSeconds > 59) {
this.clockSeconds = 0;
if (++this.clockMinutes > 59) {
this.clockMinutes = 0;
this.clockHours = (clockHours + 1) % 24;
}
}
var hours = this.clockHours;
var ampm = (hours >= 12) ? " PM " : " AM ";
if (hours > 12) { hours -= 12; }
else if (hours == 0) { hours = 12; }
clockString = (hours < 10 ? "0" : "") + hours + ":" + (clockMinutes < 10 ? "0" : "") + clockMinutes + ":" + (clockSeconds < 10 ? "0" : "") + clockSeconds + ampm + clockZone;
clockElement.innerHTML = clockString;
if (this.update == true) {
++this.clockSeconds;
if (this.firstIntervalReached == false) {
setTimeout(this.display, this.clockMilleseconds);
}
else {
setTimeout(this.display, 1000);
}
}
}
var clock = new Clock("Clock", "CST", 2, 5, 23, 321);
clock.update = true;
clock.display();

New Topic/Question
Reply



MultiQuote






|