Although it may seem like simple logic, I am currently unable to log in. Imagine I have a function called mytimer() stored in a common file that is linked to every HTML page.
mytimer(){...........................};
Now, at some point on another page, when a specific condition is met, I want this mytimer()
function to start running and continue for a set period. How can I achieve this? Any suggestions are welcome.
Thanks in advance!
Javascript code snippet:
// Set the date we're counting down to
var countDownDate = new Date(<?=$date?>).getTime();
var countdown = document.getElementById("tiles"); // get tag element
getCountdown();
var x = setInterval(function() {
getCountdown();
}, 1000);
function getCountdown() {
var now = new Date().getTime();
var distance = countDownDate - now;
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
countdown.innerHTML = "<span>" + days + "</span><span>" + hours + "</span><span>" + minutes + "</span><span>" + seconds + "</span>";
if (distance < 0) {
clearInterval(x);
document.getElementById("countdown").innerHTML = "EXPIRED";
forever = false;
}
}
function pad(n) {
return (n < 10 ? '0' : '') + n;
}
function accept() {
var id = document.getElementById("acceptbtn").getAttribute("data-id");
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if (this.readyState === 4 && this.status === 200) {
console.log(this.responseText);
}
}
xhr.open("GET", "ajax/acceptproduct.php?aid=" + id, true);
xhr.send();
alert(id);
}
<div id="countdown">
<div id='tiles'></div>
<div class="labels">
<li>Days</li>
<li>Hours</li>
<li>Mins</li>
<li>Secs</li>
</div>
</div>