Force Delay in Script

Is there a way to force a delay in a Scriptable script? I’m thinking of something akin to a sleep(1000) or wait command. I was trying to make a delay between consecutive 'Location.current()'s to get a direction.

I hope this isn’t off topic. Loving Scriptable, I’m having a lot of fun playing with it.

You can probably use setTimeout() or setInterval().

1 Like

I tried to use setTimeout() but I’m not sure what I am doing wrong. I keep getting the error Error: ReferenceError: Can't find variable: setTimeout with the below code

// get finish location after 10s delay
function wait(){ 
    setTimeout(function(){ Location.current() }, 10000)
}
let finish = wait()

My original code for getting locations, which worked but without delay, was

let finish = await Location.current()

I’ve not managed to find anything on why it isn’t working. Would anyone here have an idea?

Oops. This was a brainfart on my part. setTimeout() and setInterval() belongs to the window which is not available.

I didn’t manage to find a solution built into JavaScript so I used a function counting time that I found on GitHub.

function sleep(milliseconds) {
    var startTime = new Date().getTime();
    while (new Date().getTime() < startTime + milliseconds);
}

Thanks

3 Likes