3

I am currently trying to set up a timeout timer for a device. My initial thought is to create a time out for 15 minutes then have an interval, every minute, request the location of the device and compare it to the device's previous location. If location is greater than some threshold then reset the 15 minute timer. If the location is not greater than the threshold then the timeout continues until the 15 minutes runs out and I run some other code.

My problem is that I cant wait to get the new location in side of the interval

timeoutFunc(minutes) {
    return setTimeout(function () {
        console.log("lock device")
    }, minutes * 1000)
}

async rideTimer(minutes) {
    let coords = this.location

    this.timeOut = timeoutFunc(minutes)         // init timeout func
    let interval = setInterval(function () {
        console.log(coords)
        let newLocation = await getLocation()   // get location 
        console.log(this.location)           

        // check if location changed
        if (Math.abs(this.location[0] - coords[0]) > 0.001 || Math.abs(this.location[1] - coords[1]) > 0.001) { 
            // location did change
            clearTimeout(this.timer)            // clear timeout
            this.timeOut = timeoutFunc(minutes) // reinitialize timeout
        }// else do nothing
    }, 1000)
}
Allen
  • 31
  • 1
  • 3
  • 2
    This is my advise: Refactor your code to use Promises instead. It would be a lot easier. – Ro. Jan 25 '20 at 01:13
  • 1
    Instead of a `setTimeout` maybe you should just note the time when you start. Then in the interval check if the time is more than 15 minutes ago or reset the time to now. Also see [this thread](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) concerning accessing `this` inside a callback. – Mark Jan 25 '20 at 01:14
  • Hi, welcome to SO. Sorry, it's not possible to write code based on the problem as stated as several interpetations are possible. Can you restate the problem in high level terms not including code or pseudo-code, for example (one possible interpretation), "Set a device in motion and allow it to continue until it ceases to move a certain distance in any 15 minute period, then cause some other process to commence"? – Roamer-1888 Jan 25 '20 at 22:55
  • I actually found a nice post [here](https://stackoverflow.com/questions/42352146/understanding-javascript-promise-used-with-settimeout-and-setinterval) on stackoverflow that helped me. – Allen Jan 29 '20 at 13:55

2 Answers2

1

await can only be used with async functions.

And you can use async functions with timers:

function dummyGetData() {
  return new Promise(resolve => setTimeout(()=>resolve(new Date), 20));
}

const timer_end = Date.now() + 5000;
let timer = setInterval( async ()=> {
  const data = await dummyGetData();
  console.log(data);
  if (timer_end < Date.now()) clearInterval(timer);
},1000)
some
  • 48,070
  • 14
  • 77
  • 93
0

If you want a kinda sleep function you should do it with promises and async await, here is an dummy code:

  sleep(milliseconds) {
     return new Promise((resolve, reject) => setTimeout(resolve,milliseconds))
  }
  

   async someFunc(){
     await sleep(4000);
     console.log("work");
     return someFunc()
   }
bill.gates
  • 14,145
  • 3
  • 19
  • 47