Dyskord
Dyskord

Reputation: 387

How to insert a simple delay into javascript

So I want to do some web scraping, using AppleScript to run javascript code on a webpage. I have the following code: (edited to remove proprietary data)

function inputPunch(date_string) {
        // do task 1
        wait(3000);
        // do task 2
        wait(3000);
        // do task 3
        wait(3000);
        // do final task 
}
            
// get start date value
var date_obj = new Date(start_date_value);
date_obj.setDate(date_obj.getDate() + 2);

for(let i = 0; i < 12; i++) { 
        let cur_date_string = date_obj.toDateString();
        let cur_date_array = cur_date_string.split(" ");
        cur_date_array[2] = cur_date_array[2].replace(/^0+(\\d)|(\\d)0+$/gm, '$1$2');
        let cur_formatted_date_string = cur_date_array[1].concat(" ", cur_date_array[2], ",", cur_date_array[3]);
        console.log(cur_formatted_date_string);

        // call inputPunch at each iteration of the for loop
        inputPunch(cur_formatted_date_string);

        date_obj.setDate(date_obj.getDate() + 1);
}

The tasks are javascript UI tasks and the waits are necessary. When I run this code it says that wait is undefined and the script does nothing. I have also used await new Promise(resolve => setTimeout(resolve, ms));, which I got from How to set time delay in javascript, but then I get the error that "new" is unexpected. I would use set timeout and convert the tasks into their own functions, but the problem is that I need the initial inputPunch to also wait and not go onto the next iteration of the for loop until the first one is finished. Any thoughts on how to do this? I'm willing to restructure the code to get it to work as desired

Upvotes: 0

Views: 143

Answers (2)

Scott Marcus
Scott Marcus

Reputation: 65808

Very simple way to cause a delay is to use a timer.

Here's an example:

function callBack(){
  alert("Time's up!");
}


// First arg is what function to call
// Second arg is how long (in milliseconds) to wait
setTimeout(callBack, 2000);

"I would use set timeout and convert the tasks into their own functions, but the problem is that I need the initial inputPunch to also wait and not go onto the next iteration of the for loop until the first one is finished."

It's not entirely clear what you want your code flow to be and I suspect whatever you are trying to accomplish within your loop should be done without the loop entirely, but that's not a problem if you put your timer(s) in the right spots.

setTimeout(inputPunch, 2000);

function inputPunch(date_string) {
  loop();
  setTimeout("", 2000);
  loop();
  setTimeout("", 2000);
  loop();
  setTimeout("", 2000);
}

function loop(){     
  // get start date value
  var date_obj = new Date(start_date_value);
  date_obj.setDate(date_obj.getDate() + 2);

  for(let i = 0; i < 12; i++) { 
    let cur_date_string = date_obj.toDateString();
    let cur_date_array = cur_date_string.split(" ");
    cur_date_array[2] = cur_date_array[2].replace(/^0+(\\d)|(\\d)0+$/gm, '$1$2');
    let cur_formatted_date_string = cur_date_array[1].concat(" ", cur_date_array[2], ",", cur_date_array[3]);
    console.log(cur_formatted_date_string);

    // call inputPunch at each iteration of the for loop
    inputPunch(cur_formatted_date_string);

    date_obj.setDate(date_obj.getDate() + 1);
  }
}

Upvotes: -1

Ata Parvin Ghods
Ata Parvin Ghods

Reputation: 27

there is no sleep function in JS but it's easy to create your own. So your code will look like this:

const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay))

async function inputPunch(date_string) {
   // do task 1
   await sleep(3000)
   // do task 2
   await sleep(3000)
   // do task 3
   await sleep(3000)
   // do final task
}

// get start date value
var date_obj = new Date(start_date_value)
date_obj.setDate(date_obj.getDate() + 2)

// Put this in function so you can use async/await
async function bootstrap() {
   for (let i = 0; i < 12; i++) {
      let cur_date_string = date_obj.toDateString()
      let cur_date_array = cur_date_string.split(' ')
      cur_date_array[2] = cur_date_array[2].replace(
         /^0+(\\d)|(\\d)0+$/gm,
         '$1$2'
      )
      let cur_formatted_date_string = cur_date_array[1].concat(
         ' ',
         cur_date_array[2],
         ',',
         cur_date_array[3]
      )
      console.log(cur_formatted_date_string)

      // call inputPunch at each iteration of the for loop
      await inputPunch(cur_formatted_date_string)

      date_obj.setDate(date_obj.getDate() + 1)
   }
}

bootstrap()

Upvotes: 0

Related Questions