setTimeout missing

Looks like the JavaScript engine inside Scriptable doesn’t know what setTimeout is. Anyone know if this is a known issue/if there is a workaround? The PureScript compiler seems to assume that setTimeout is an available global function so my code just fails with a ReferenceError :frowning:

Isn’t that a Window method? If so you it is probably that you don’t have a browser window to match it against. Scriptable isn’t a web browser in that sense.

1 Like

Yeah, you’re right. We would need our own implementation in the JS runtime, just as Node does. Still trying to come up with a way to polyfill the feature in the meantime - maybe a network request that is known to be very fast that repeats until the duration has expired

1 Like

You could achieve a similar result with promises. Here’s a simple example of waiting 10 seconds before executing. You could expand this to run some code every “n” seconds

async function timeout() {
   var delay = 10000; // milliseconds
   var before = Date.now();
    while (Date.now() < before + delay) {};
    return true;
 }

timeout().then( () => { console.log('hello'); })

Whilst this will work, it’ll be blocking, no? I’m not sure I follow what the promise achieves that the while loop alone does.

Further it will burn battery. It’s a tight loop.

1 Like

I’ve just posted a real asynchronous solution to the missing setTimout here. Essentially, it leverages a WebView to kick off a timeout and return a promise that resolves when it, um, times out. Note this needs Scriptable 1.1.4 (in beta ATM).

2 Likes