The problem is that although the countdown is displayed correctly when the page is first loaded, when the page is refreshed in the browser the counter goes to zero.
Can anyone point out where I'm going wrong?
I'm using three files: index.html, ajaxcountdown.js & servertime.php.
index.html...
- Code: Select all
<html>
<head>
<title></title>
<SCRIPT language=JavaScript src="functions.js"></SCRIPT>
</head>
<body>
<P>Countdown ends:</P>
<script src="ajaxcountdown.js"></script>
</body>
</html>
ajaxcountdown.js...
- Code: Select all
document.writeln('<span class="" style="color:#f00;font-size:39px;font-weight:bold;" id="countdowntimer"></span>');
InitiateTimer();
function UpdateTimer()
{
TimeLeft--;
strText = ''
if (TimeLeft <= 0)
{
strText = "NOW!!!";
}
else
{
strText = parseInt(TimeLeft/86400)+ " Days "+parseInt(TimeLeft/3600%24)+ " Hours "+parseInt(TimeLeft/60%60)+ " Minutes "+parseInt(TimeLeft/1%60)+ " Seconds ";
}
document.getElementById('countdowntimer').innerHTML = strText;
}
var TimeLeft = -1;
function InitiateTimer()
{
var http;
var http = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http = new XMLHttpRequest();
if (http.overrideMimeType) {
http.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http) {
alert('Agent unable to establish communication :( ');
return false;
}
http.open("GET", "servertime.php", true);
http.onreadystatechange = function()
{
if (http.readyState == 4) {
//var response = http.responseText;
//a = response.split('\n');
//TimeLeft = a[1];
TimeLeft = http.responseText;
setInterval("UpdateTimer()",1000);
}
}
http.send(null);
}
servertime.php...
- Code: Select all
<?php
// The number of the month when the countdown ends (1-12)
$month=12;
//The number of the day when the countdown ends (1-31)
$day=5;
//The number of the year when the countdown ends (yyyy)
$year=2008;
// How do you want the countdown displayed?
// 1 = days
// 2 = hours
// 3 = minutes
// 4 = seconds
$display = 4;
// DON'T EDIT AFTER THIS LINE
// --------------------------
$target = mktime(0, 0, 0, $month, $day, $year);
$today = time ();
$difference =($target-$today);
if ($display == 1) { echo ($difference/86400); }
if ($display == 2) { echo ($difference/3600); }
if ($display == 3) { echo ($difference/60); }
if ($display == 4) { echo ($difference); }
?>
Any help will be much appreciated!




