This is pretty simple logic may be , but i am out of login at this moment . let's say i have a function called mytimer() which is in a generic file. i mean every html page is linked to that files .
mytimer(){...........................};
now somewhere in another page , when my condition gets true .or something like this , i wan this mytimer()
to be fired and keep it running for given time.
How do i do this . ? any advice would appreciated
TIA
javascript code
// 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 the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("countdown").innerHTML = "EXPIRED";
forever = false;
}
}
function pad(n) {
return (n < 10 ? '0' : '') + n;
}
//when the accept button is triggered , i want above block code to be run like countdown timer
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>