I am making a chrome extension for web automation.The first step is to get a list of sites and instructions from a server in a delimiter-format. Once the list is obtained it is divided into an array that i call "siteArray".
The site array is then divide into another array i call "instructionsArray" and among those items in the array one of them is the duration spent on the site i.e instructionsArray[5] has the value of "10" seconds. {the duration is not the same for all the sites}
My question arises in how to delay(implementing duration)
One implementation I found is using a sleep function which turns out to be inefficient as it is just a long for loop
see code:
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
and then:
getlist();
for each(siteArray){
instructionsArray = siteArray[count].split(delimiter);
gotoUrl(instructionsArray[1]);
sleep(instructionsArray[5]);
count++;
}
Where
- getlist() fetches a list of instructions and splits into the siteArray .
- gotoUrl() changes url.
- sleep() runs the sleep function.
Is there a better way to implement a duration/wait/delay.