Help

Re: I need a DELAY method & setTimeout() is undefined (not found Window)

2220 0
cancel
Showing results for 
Search instead for 
Did you mean: 
David_Koontz
6 - Interface Innovator
6 - Interface Innovator

Hey,

I’m a newbie with JavaScript so this might be obvious …

I need a delay(ms) method to slow down the script after 5 API calls (limit 5/min.). Problem is all the JavaScript help on the intertube points to the setTimeout() method of the Window object. But it appears that Window is not instantiated inside the Airtable code block, therefore, no setTimeout() method. What’s a good workaround for this?

What might I be missing - other methods?

Thanks
David

53 Replies 53
Perrin_Romney
5 - Automation Enthusiast
5 - Automation Enthusiast

@David_Koontz @Bill_French  Do know if this has been solved yet? With this code:

 

function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
await timeout(1000); // 1 sec pause
}

 

I am throwing this error:

 

ERROR
ReferenceError: setTimeout is not defined
    at <anonymous> on line 16
    at timeout on line 16
    at main on line 19

 

 I'm not a professional code monkey as much as I try to be, so I may just need to load the library or something, not sure.

Sannit_Vartak
5 - Automation Enthusiast
5 - Automation Enthusiast
function delay(ms) {
var limit = new Date();
limit = limit.setMilliseconds(limit.getMilliseconds() + ms);
while ((new Date()) < limit) {
    // do nothing
    ;
}
}
delay(20000); //delay 20 second

 

This will work for Delay just replace value inside Delay()

Dean_Arnold
7 - App Architect
7 - App Architect

Hi All,

Common pain point - Wish they'd just add a delay action, a la Zapier!..

That said, what would be the risk of using a script like this, which seems to run just fine...

function wait(ms) {
return new Promise(resolve => {
const startTime = new Date().getTime();
while (new Date().getTime() < startTime + ms);
resolve();
});
}

async function main() {
console.log('Waiting for 20 seconds...');
await wait(20000);
console.log('Done!');
}

main();
Lu
6 - Interface Innovator
6 - Interface Innovator

A very quick and dirty DIY solution for those who have access to a public web server that runs PHP could be as simple as creating a script with the following:

<?php
sleep(max(0, (int) $_POST['seconds']));

Then do a fetch on it in your automation like so:

await fetch('https://your-site.example/delay.php', {
    method: 'POST',
    body: new URLSearchParams([
        seconds: 55
    ])
});

Seems pretty bizarre that Airtable would rather have users max out their servers' CPUs than provide a sleep function. They could provide a simple network service like slowwly, that's only reachable from automations, to avoid modifications to their JavaScript execution environment.