Updated Question: Despite the larger issue, can someone explain why the sleep function in the code below doesn't provide a one-hour pause in execution? Understanding this would be greatly appreciated.
I am trying to trigger a JavaScript function that runs continuously for one month within a Chrome extension but pauses its execution for one hour every day between 8:15am and 9:15am.
Here is my attempted code, but the delay from the sleep() function is not working:
function(){ //triggered on button press, an extremely long process
function sleep(miliseconds) {
var currentTime = new Date().getTime();
while (currentTime + miliseconds >= new Date().getTime()) {
}
}
var data_array = ["fake_data1", "fake_data2", /*...*/ "fake_data923421"];
for (let i=0; i<data_array.length; i++){
var today = new Date();
var hours = today.getHours();
var minutes = today.getMinutes();
if(hours >= 8 && minutes >= 15){
sleep(3600000);
}
//do things if it's not 8:15-9:15am
setTimeout(function() {
//things here happen as expected with setTimeout delay
//have had issues using setTimeout sometimes, but not consistently
//hence the use of the sleep() function (credit goes to SO)
}, (i+1)*10000);
}
});
Important Notes: - I am open to alternative approaches to achieving this goal. - There are valid reasons for using JavaScript and running the script constantly for a month. - No users will interact with the page, so UI responsiveness is not relevant
Bonus Points - considering this is in a Chrome extension, if there is a way to store data without using an array and mark each value processed to resume from where it left off after stopping, that would be helpful. While I could set up a database or web service, I prefer a quick and easy solution for this one-time, non-production code.