Help

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

6099 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
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.

I was also using only block-native features and no external calls.

Have you been submitting more than 5 calls per second to createRecordsAsync(), updateRecordsAsync() or deleteRecordsAsync()? That’s how I ran up against what I believe to be an unpublished rate limit. In my script, I didn’t need to await the return values so I was running them in parallel instead of in series.

openside
10 - Mercury
10 - Mercury

Remove window. :slightly_smiling_face: should just be calling setTimeout() like in my code example

David_Koontz
6 - Interface Innovator
6 - Interface Innovator

Thanks @Justin_Barrett - again, the restriction is on the API I am calling Alpha Vantage.

I’ve got code that is working this morning. From @Bill.French

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

await timeout(1000); // 1 sec pause

But I swear I had the same code last night and it did NOT work… so I’m stumped as to what magic dust makes it work today. Even with the IDE given the error codes about setTimeout().

I’m using a mix of create and update operations, though in my case I need to use await on all of them because I’m linking between various records as they’re created, so they probably aren’t triggering in quick succession.

I doubt that you could run more than 5 requests per second when using await. I didn’t see the problem until I started to run the requests in parallel. I consistently got the generic “Something went wrong” error. Then I slowed down my script to just over 5 requests per second with no other changes and the problem disappeared. I’d be curious to see if anyone else can replicate the issue.

That’s exact same code I shared with you originally :winking_face:

And where I found it as well. But there is an issue here given the subtle indicator from the editor that this is not supported. And I wonder if this morning’s API issues align with @David_Koontz failure experiences.

Do you think that the wavy red underlines in the editor means that setTimeout() is not supported? I get those wavy red underlines all the time for things that clearly are supported, so I tend to ignore the wavy red underlines after a while. Or is there something else?

For me, setTimeout() works as expected. It waits the specified amount of time and then runs the provided function with the provided parameters.

setTimeout(echoText, 1000);
setTimeout(echoText, 2000, "Hello, World, again!");
await input.textAsync("Just something to keep the script from ending");
function echoText(myText = "Hello, World!") {
    output.text(myText);
}

That’s exactly what it is suggesting.

The **red underline** has been a tradition in IDE for 20 years… it has various meanings… but general a reason for the developer to stop and fix it.

I wonder why the Airtable IDE (tool) is fooled??? I’m guessing it is the difference between parse time and run time - the setTimeout() method must be found at runtime for it to work… but at code parse (interpretation) time - it’s an error.

One of my JS experts mentioned this aspect this morning.