Skip to main content

Create a delay with a script

  • November 13, 2020
  • 7 replies
  • 27 views

Hi Everyone!

I’m trying to set a 5-10 seconds delay for an automation.

I’ve checked this post :
https://community.airtable.com/t/i-need-a-delay-method-settimeout-is-undefined-not-found-window/28125

And I’ve tried the following script:

function timeout(ms) {
   return new Promise(resolve => setTimeout(resolve, ms));
 }
await timeout(50000)//waits 5 seconds

The editor gives an error and my automation can’t complete.

Do you have any clue of how I can (A) fix this issue or (B) set a delay in a different way ?

Regards,

7 replies

kuovonne
Forum|alt.badge.img+27
  • Brainy
  • 6001 replies
  • November 13, 2020

Is a longer delay okay? If so, check out this support article.


Forum|alt.badge.img+19
  • Inspiring
  • 3264 replies
  • November 13, 2020

Overtly blocking a non-blocking architecture is generally a risky idea; it often leads to brittle and unexpected outcomes.

It’s always good to explain why you need a delay because there may be different pathways to achieve the automated process without a delay.


  • Author
  • New Participant
  • 3 replies
  • November 13, 2020
Bill_French wrote:

Overtly blocking a non-blocking architecture is generally a risky idea; it often leads to brittle and unexpected outcomes.

It’s always good to explain why you need a delay because there may be different pathways to achieve the automated process without a delay.


You’re right, here is my full problem.

I’m using an Airtable Web Clipper to get a screenshot
Then I’m using one automation (on the new record created) to use the screenshot as an attachment to a Twitter message.

The problem is that the screenshot takes a few seconds to completely download into the new record. Meanwhile, the automation is already starting and finally, the tweet isn’t generated because Airtable considers that there’s no image in the field.

Introducing a short delay, would allow me to bypass the initial problem I have.


Forum|alt.badge.img+19
  • Inspiring
  • 3264 replies
  • November 13, 2020
Cedric_NIEUTIN wrote:

You’re right, here is my full problem.

I’m using an Airtable Web Clipper to get a screenshot
Then I’m using one automation (on the new record created) to use the screenshot as an attachment to a Twitter message.

The problem is that the screenshot takes a few seconds to completely download into the new record. Meanwhile, the automation is already starting and finally, the tweet isn’t generated because Airtable considers that there’s no image in the field.

Introducing a short delay, would allow me to bypass the initial problem I have.


Perhaps you could force the web clipper step to perform one additional step - set a field as “Step One Complete” and then force the next step as a dependency on the final step of the previous process.

Another idea - since you are adept with script, why not simplly use a while (!notSaved) loop or a looped try-catch process checking for the availability of the clipped image?


Forum|alt.badge.img+12
  • Participating Frequently
  • 93 replies
  • December 2, 2020

Hi @Cedric_NIEUTIN,

The next solution is not the best way to do this, but it was the only way I find, as they blocked the “settimeout” function.

function delay(ms) {
var limit = new Date();
limit = limit.setMilliseconds(limit.getMilliseconds() + ms);
while ((new Date()) < limit) {
    // do nothing
    ;
}

delay(1000); //delay 1 second

I found this on StackOverflow, so credits for that guy, but I really think it´s awful !!! haha

The thing is that this blocks entirely the script and it´s not very recommended, but in my case, I only needed a 1-second delay so it was better than building another automation workflow.

Hope that it helps

Sergio


SergioCodes wrote:

Hi @Cedric_NIEUTIN,

The next solution is not the best way to do this, but it was the only way I find, as they blocked the “settimeout” function.

function delay(ms) {
var limit = new Date();
limit = limit.setMilliseconds(limit.getMilliseconds() + ms);
while ((new Date()) < limit) {
    // do nothing
    ;
}

delay(1000); //delay 1 second

I found this on StackOverflow, so credits for that guy, but I really think it´s awful !!! haha

The thing is that this blocks entirely the script and it´s not very recommended, but in my case, I only needed a 1-second delay so it was better than building another automation workflow.

Hope that it helps

Sergio


That help me a lot!! Thank you.

There is a missing “}” at the end, here is the right code:

function delay(ms) {
    var limit = new Date();
    limit = limit.setMilliseconds(limit.getMilliseconds() + ms);
    while ((new Date()) < limit) {
        // do nothing
        ;
    }
}
    
    delay(9000); //delay 9 second

Forum|alt.badge.img+16

maybe a slight hack around this - i simply put in a formula field which = 1 when there is an attachment in the attachment field

and then run the automation from the formula being updated rather than the attachment field …

seems to work with a 20MB file…


Reply