i have a question about a javascript/php cron issue. well its not an issue per se but in my case it is. i have the below javascript that sets a cookie and counts down till the end of that cookie's time. in my case its 10 minutes. I also have a cron file that runs every 10 minutes. I would like to synchronize the countdown with the cron file.
The below script works well when i call it from the page however it counts down from the time the user visits the page and its out of sync with the cron file im running.
i understand this is not a php question but the pages im displaying the scripts are php.
Any pointers for me? Help would be greatly appreciated. Thank you for reading.
All i have is below.
<div id="countre4">
<script type="text/javascript">
function setCookie(c_name, value)
{
var exdate = new Date();
exdate.setDate(exdate.getDate() + 14);
var c_value = escape(value) + "; expires=" + exdate.toUTCString();
document.cookie = c_name + "=" + c_value;
}
function mycountre(countdownId, countdownTarget, countdownSeconds, countdownLooping){
var countre = document.getElementById(countdownId); // get html element
if (!countre) {
return;
}
var target = countdownTarget; // target time
var intervalId; // id of the interval
setCookie("time", target);
// update function
function updatecountre(){
var time = Math.floor((target - new Date().getTime()) / 1000); // countdown time in seconds
if (time < 0) { // if countdown ends
if (countdownLooping) { // if it should loop
target += 1000 * countdownSeconds; // set new target time
time = Math.floor((target - new Date().getTime()) / 1000); // recalculate current time
setCookie("time", target);
} else { // otherwise
clearInterval(intervalId); // clear interval
time = 0; // set time to 0 to avoid displaying negative values
}
}
// split time to seconds, minutes and hours
var seconds = '0' + (time % 60);
time = (time - seconds) / 60;
var minutes = '0' + (time % 60);
time = (time - minutes) / 60;
var hours = '0' + time;
// make string from splited values
var str = hours.substring(hours.length - 2) + ':' + minutes.substring(minutes.length - 2) + ':' + seconds.substring(seconds.length - 2);
countre.innerHTML = str;
}
intervalId = setInterval(updatecountre, 200); // start interval to execute update function periodically
};
var parts = document.cookie.split(';');
var cookie = new Date().getTime() + 1000 * 15 * 60;
for (var i = 0; i < parts.length; ++i) {
if (parts[i].trim().indexOf('time') == 0) {
var value = parts[i].trim().split('=');
cookie = parseInt(value[1]);
}
}
mycountre(
'countre4', // id of the html element
cookie,
10 * 60, // time in seconds (10min here)
true // loop after countdown ends?
);
</script>
</div>