Help

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

Topic Labels: Scripting extentions
17678 53
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

Yes @Bill.French I’ve checked and rechecked … same code! I can’t recreate the state of my system the other night when it was failing and I chased a rabbit down the wrong trail… but I swear it was not working… now it could be one of many reasons (maybe none the timeout method) cause I’m a newbie with embedding JS like this.

But now that it’s working I’m continuing my attempt to get stock quotes via 3rd party (Alpha Vantage http://alphavantage.co) - I’d like to know what interfaces I’d need to support to make my code a first rate Airtable block. (any info?)

Simple answer - Script Block is an infant, not even a toddler yet. Surprisingly, it runs very well despite it being only a few weeks old.

Thanks for the info. I don’t use an IDE.

I have seen wavy red underlines in other contexts for years (Word documents), and they usually mean that the grammar checker thinks the section has a grammar problem. Sometimes there really is a grammar problem, and sometimes there isn’t. When I know the grammar is correct, I ignore the wavy red underlines.

My code editor does not have a grammar checker, but it does color-code text according to a selected grammar, which helps in discovering some grammar problems.

In the Airtable scripting block, I assumed that the wavy red underlines were also a grammar/vocabulary checker. I find that Airtable editor’s grammar/vocabulary checker is so overzealous that it gets in my way. I do use it to check to make sure the grammar and vocabulary is correct, but I also feel free to ignore its suggestions.

I have not seen wavy red underlines directly indicating that a particular feature is unsupported. Rather I take it to simply mean that the grammar checker does not understand the section, which may or may not mean that the features lack support.

I’ve been very happy with Scripting block, and I’ve gotten a lot of enjoyment out of it. I’m not surprised at how well it runs–I expected that level of quality from Airtable.

LOL! I believe you. This is software.

Well, you need URLFetch() to pull that off, but “first rate” is a subjective question.

David_Koontz
6 - Interface Innovator
6 - Interface Innovator

Yes the Scripting Block does a really good job for being such a new feature. I AM AMAZED. I worked with it for hours and never had any issue. For a web app - this is unheard of reliability - not seen since Netware was turned off. Someone knows how to write some great code that recovers all it’s memory well.

How are you calling the other API? One of the APIs I use doesn’t allow you to pass your key through the URL and when I try to install https, it doesn’t seem to work.

David_Koontz
6 - Interface Innovator
6 - Interface Innovator

In the script (JavaScript code):

let info = await fetch(https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=${symbol}&apikey=${API_KEY});

But the format of the URL is API specific - this is for Alpha Ventures. You should read their docs/examples or call them.

Thanks for the info. This company doesn’t actually have API documentation which makes trying to find what to pass very frustrating. Their example shows “api-key” but it only works when using curl… I’ve reached out to their team and it seems like their knowledge of how it works is as good as their non-existent documentation.

Paste the CURL example here and I’ll bet we can transform the requirements into Airtable speak.

Thanks guys!

Here it is:
curl --header “api-key: [apikeytxt]” -X GET "https://api-hazards.atcouncil.org/public/v1/wind.json?lat=[lat variable]&lng=[long variable]

Something like:

let url = '"https://api-hazards.atcouncil.org/public/v1/wind.json?lat=[lat variable]&lng=[long variable]';
let getOptions = {
    method: "get",
    headers: {
        'api-key' : 'apiKeyHere',
        'Accept'  : 'application/json',
    }
}
let postResults = await fetch(url, getOptions);

That worked! Thank you, sir!

With how much our business uses random APIs like this, I plan on sharing this and another script we recently created in the forum once they’re up and running.

Adam_Grant
4 - Data Explorer
4 - Data Explorer

This strategy still doesn’t seem to work on my end. Here’s a complete example to demonstrate. Am I doing something wrong on my end?

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

async function test() {
    output.text("foo");
    await timeout(300);
    output.text("bar");
}

test();

Expected: “foo” prints, then “bar” 300ms later.
Actual: “foo” prints, “bar” never does.

You need to await the test function when you call it.

await test();

Thank you very much @openside,
I was just needing a DELAY and you’ve helped very much !

As I read the all thread coming from this one:

I would add that
window is globalThis here in the Script-Block.

Thank you and good night (GMT),

olπ

Wylan_Osorio
4 - Data Explorer
4 - Data Explorer

image

It is not working on my script. Do you guys have solution already for this?

Welcome to the Airtable community!

It looks like you are using an automation script. The scripts which have been able to use setTimeout have been scripts run in Scripting app. There are a few differences between running a script as an automation and running a script in Scripting app. The ability to use setTimeout appears to be one of these differences.

Do you know any alternative to put delay in automation script?

Why do you need a delay in your automation script? That might influence the approach. Two common approaches are reworking the trigger, or splitting the automation into two automations.