0

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>
Bhuwan
  • 16,525
  • 5
  • 34
  • 57
Atik Hashmee
  • 393
  • 3
  • 12
  • by **keep it running for given time** do you mean `setTimout`? , You need to explain what mytimer does – Yasir Feb 18 '18 at 07:12
  • sir , i have updated the code what i really want to do – Atik Hashmee Feb 18 '18 at 07:30
  • you need a recursive call with a setTmeout as follows: `if (distance < 0) { clearInterval(x); document.getElementById("countdown").innerHTML = "EXPIRED"; forever = false; } else { setInterval(function() { getCountdown(); }, 1000); }` – Yasir Feb 18 '18 at 07:53

2 Answers2

1

You could use recursion.

const TIMEOUT_MS = 1000

let keepGoing = true;

function doSomething() {


    // do your logic here - if you want to change keep going to false to stop



    if (keepGoing) {
        setTimeout(doSomething, TIMEOUT_MS);
    }
}

doSomething();
p0wdr.com
  • 307
  • 2
  • 4
  • I am sorry , but i have to tell this code is not making anything . so far i know setTimeout execute the function after given time . how does this work forever ?? can you be more specific ?? – Atik Hashmee Feb 18 '18 at 07:37
  • @AtikHashmee The trick is that the `setTimeout` calls `doSomething` again, so the function keeps calling itself while the `keepGoing` flag is true - this is the essence of recursion. It's not doing anything because there is no logic, as the author suggests put some in place of the comment and see (e.g. just a mere `console.log('meh')`). There is also [`setInterval`](https://www.w3schools.com/jsref/met_win_setinterval.asp) but it doesn't wait until the logic is processed (which recursive `setTimeout` does - see details in this [SO answer](https://stackoverflow.com/a/729943/8555393)) – Tomas Varga Feb 18 '18 at 08:58
0

It sounds like you want to use the window.setTimeout function.

var myVar;

function myThingToDo() {
    // Do stuff and things

    // Then call yourself again after a timeout.
    myVar = setTimeout(myThingToDo, 3000);
}

// Call this where/whenever you want to kill the loop
function myStopFunction() {
    clearTimeout(myVar);
}

Here's more info: https://www.w3schools.com/jsref/met_win_settimeout.asp

Gerik
  • 698
  • 3
  • 11