Help

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

9992 2
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

57 Replies 57
openside
10 - Mercury
10 - Mercury

This is the function I use:

    function timeout(ms) {
       return new Promise(resolve => setTimeout(resolve, ms));
    }

then you can use like:
await timeout(5000)//waits 5 seconds

David_Koontz
6 - Interface Innovator
6 - Interface Innovator

hmmm…

@openside - why do I get an error when I try to use that in a Airtable block.
“Cannot find setTimeout()”

Screen Shot 2020-03-17 at 10.21.29 AM

I don’t believe the Scripting block has the limit on API calls that you would have if you were writing API code to be executed elsewhere, mainly because you’re not calling the API when using the Scripting block. The API is the means of accessing Airtable when you’re outside of the Airtable interface. The Scripting block is internal, so there are no such restrictions.

David_Koontz
6 - Interface Innovator
6 - Interface Innovator

Other way around @Justin_Barrett; I’m using a 3rd party API with a restriction. Alpha Vantage - for stock market prices.

Do you know why the setTimeout() is not found?

Sorry. I thought you were referring to the Airtable API, not a third party API.

I’m just getting reacquainted with JavaScript myself, and haven’t yet hit a situation where I’d need a delay, so I’m not sure why setTimeout() isn’t available.

openside
10 - Mercury
10 - Mercury

@David_Koontz - their editor gives a warning, but the code runs fine. Their editor just doesn’t recognize it. You can tell it to ignore the error warning if you want by right clicking over it and clicking on the ‘Quick Fix’

I’ve run up against what I believe to be a rate limit in the scripting block API. When I modified my script to slow calls down to about 5 requests per second, the script worked.

David_Koontz
6 - Interface Innovator
6 - Interface Innovator

I’m getting this Error:

ERROR

ReferenceError: Can’t find variable: window

asyncFunctionResume@[native code] main anonymous s@blob:https://block--v-xd-mn-xix-b-lud-p5--06e4o42.airtableblocks.com/467c6f74-ac94-41bc-9fe7-45746e655847...

Any ideas why it works for you but not me?

Strange, because I’ve got a script that does a ton of stuff to my base, and I haven’t encountered any timing-related issues that I know of. Again, this is just using block-native features to only read and modify Airtable records, not calls to external APIs.