wsjb78
08-29-2003, 07:34 AM
Hiya,
I'm not a JS guru but I need to have a countdown script on one of my sites. Basically it should countdoung to a certain hour and then start all over again.
I went to www.hotscripts.com and found a great script which does exactely that. However there is one drawback in it. The script takes the user's system time and not the servertime. So I need to adjust that.
js.countdownjs.php
var myCountdown = new Array();
var repeat = false;
function offset() {
<?
$currentDate = mktime();
$currentDate = $currentDate*1000;
?>
// Get user's computer time
var systemtime = new Date();
// Calculate offset between the two
var offset = <? echo $currentDate; ?>-systemtime;
}
function checkPlural(noun, value) {
noun = ((value == 1) || (value == 0)) ? noun : (noun += "s");
return noun;
}
function updateDisplay(text, id) {
var tag = document.getElementById(id);
if (tag.firstChild) {
tag.firstChild.nodeValue = text;
}
else {
textNode = document.createTextNode(text);
tag.appendChild(textNode);
}
return;
}
function doCountdown(offset) {
for (i = 0; i < myCountdown.length; i++) {
if (!myCountdown[i].expired) {
var currentDate = today = new Date(today.getTime() + offset)
var eventDate = myCountdown[i].eventDate;
var timeLeft = new Date();
timeLeft = eventDate - currentDate;
msPerDay = 24 * 60 * 60 * 1000;
msPerHour = 60 * 60 * 1000;
msPerMin = 60 * 1000;
msPerSec = 1000;
daysLeft = Math.floor(timeLeft / msPerDay);
hoursLeft = Math.floor((timeLeft % msPerDay) / msPerHour);
minsLeft = Math.floor(((timeLeft % msPerDay) % msPerHour) / msPerMin);
secsLeft = Math.floor((((timeLeft % msPerDay) % msPerHour) % msPerMin) / msPerSec);
day = checkPlural("day", daysLeft);
hour = checkPlural("hour", hoursLeft);
minute = checkPlural("minute", minsLeft);
second = checkPlural("second", secsLeft);
if ((daysLeft == 0) && (hoursLeft == 0) && (minsLeft == 0) && (secsLeft == 0)) {
updateDisplay(myCountdown[i].onevent, myCountdown[i].tagID);
}
else {
if (daysLeft <= -1) {
updateDisplay(myCountdown[i].afterevent, myCountdown[i].tagID);
myCountdown[i].expired = true;
}
else {
updateDisplay("offset: " + offset + " ---- " + hoursLeft + " h " + minsLeft + " min " + secsLeft + " s " +
myCountdown[i].event, myCountdown[i].tagID);
repeat = true;
}
}
}
}
if (repeat) {
repeat = false;
window.setTimeout("doCountdown()", 1000);
}
else {
return;
}
}
Ok, some explanation:
1.) The ending is .php and loading it works fine!
2.) Because the ending is .php I can use PHP within the JS
3.) The variable "offset" in the function "offset" is calculated correctly (I think) -- I have created the function offset in the hope I can pass the value for offset into the other function
The problem is I cannot pass that value of "offset" into the "doCountdown" funciton.
Anyone knows how to do that?
I'm not a JS guru but I need to have a countdown script on one of my sites. Basically it should countdoung to a certain hour and then start all over again.
I went to www.hotscripts.com and found a great script which does exactely that. However there is one drawback in it. The script takes the user's system time and not the servertime. So I need to adjust that.
js.countdownjs.php
var myCountdown = new Array();
var repeat = false;
function offset() {
<?
$currentDate = mktime();
$currentDate = $currentDate*1000;
?>
// Get user's computer time
var systemtime = new Date();
// Calculate offset between the two
var offset = <? echo $currentDate; ?>-systemtime;
}
function checkPlural(noun, value) {
noun = ((value == 1) || (value == 0)) ? noun : (noun += "s");
return noun;
}
function updateDisplay(text, id) {
var tag = document.getElementById(id);
if (tag.firstChild) {
tag.firstChild.nodeValue = text;
}
else {
textNode = document.createTextNode(text);
tag.appendChild(textNode);
}
return;
}
function doCountdown(offset) {
for (i = 0; i < myCountdown.length; i++) {
if (!myCountdown[i].expired) {
var currentDate = today = new Date(today.getTime() + offset)
var eventDate = myCountdown[i].eventDate;
var timeLeft = new Date();
timeLeft = eventDate - currentDate;
msPerDay = 24 * 60 * 60 * 1000;
msPerHour = 60 * 60 * 1000;
msPerMin = 60 * 1000;
msPerSec = 1000;
daysLeft = Math.floor(timeLeft / msPerDay);
hoursLeft = Math.floor((timeLeft % msPerDay) / msPerHour);
minsLeft = Math.floor(((timeLeft % msPerDay) % msPerHour) / msPerMin);
secsLeft = Math.floor((((timeLeft % msPerDay) % msPerHour) % msPerMin) / msPerSec);
day = checkPlural("day", daysLeft);
hour = checkPlural("hour", hoursLeft);
minute = checkPlural("minute", minsLeft);
second = checkPlural("second", secsLeft);
if ((daysLeft == 0) && (hoursLeft == 0) && (minsLeft == 0) && (secsLeft == 0)) {
updateDisplay(myCountdown[i].onevent, myCountdown[i].tagID);
}
else {
if (daysLeft <= -1) {
updateDisplay(myCountdown[i].afterevent, myCountdown[i].tagID);
myCountdown[i].expired = true;
}
else {
updateDisplay("offset: " + offset + " ---- " + hoursLeft + " h " + minsLeft + " min " + secsLeft + " s " +
myCountdown[i].event, myCountdown[i].tagID);
repeat = true;
}
}
}
}
if (repeat) {
repeat = false;
window.setTimeout("doCountdown()", 1000);
}
else {
return;
}
}
Ok, some explanation:
1.) The ending is .php and loading it works fine!
2.) Because the ending is .php I can use PHP within the JS
3.) The variable "offset" in the function "offset" is calculated correctly (I think) -- I have created the function offset in the hope I can pass the value for offset into the other function
The problem is I cannot pass that value of "offset" into the "doCountdown" funciton.
Anyone knows how to do that?