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
2
Answers
there is no sleep function in JS but it’s easy to create your own. So your code will look like this:
Very simple way to cause a delay is to use a timer.
Here’s an example: