How do I use setTimeout function in my JS script

Viewed 29

I'm trying to create a script in Airtable linked to a button. The process is as follows:

  1. Button within Airtable pressed
  2. If the checkbox field 'Jira switch' is false, then turn it to true
  3. Wait for 2 seconds, reset the field to false

This is the script I've written using the setTimeout() function:

let table = base.getTable("XOS"); //The name of the table you're in here
let record = await input.recordAsync('Pick a record', table);
if (record) {
   
 if (record.getCellValue("Jira switch")===false) {
    table.updateRecordAsync(record, {'Jira switch': true});
    output.text('checkbox ticked');
 } 
 
const myTimeout = setTimeout(timeDelay, 2000);
   function timeDelay()
      table.updateRecordAsync(record, {'Jira switch': false});
      output.text('2 second interval');
    
}

When I write the code I get an error that says "cannot find name setTimeout". As a solution, it suggests "add missing function declaration 'setTimeout'"

How do I declare this function so it can function with my code?

2 Answers

I think ther is a problem in your syntax. After function timeDelay() you forgot to add curly bracket to declare what code are inside the function.

missing curly bracket{

and what is if(record){doing or where is closing of it}?

let table = base.getTable("XOS"); //The name of the table you're in here
let record = await input.recordAsync('Pick a record', table);
   /*if (record) {*========> where is it ending*/ 
 if (record.getCellValue("Jira switch")===false) {
    table.updateRecordAsync(record, {'Jira switch': true});
    output.text('checkbox ticked');
 } 
 
const myTimeout = setTimeout(timeDelay, 2000);
   function timeDelay() {/*========> missing curly bracket*/
      table.updateRecordAsync(record, {'Jira switch': false});
      output.text('2 second interval');
    
}

Related