I am trying to send regular heartbeats from the frontend in a javascript web worker (using Angular 15.2) but I noticed that its execution stops after some time, when the focus is not anymore on the browser (MS Edge here), or the computer screen is locked.
I used the following code in the Web Worker
let count = 0
while (true)
{
count++;
console.log("while background log " + count);
console.log(new Date().toLocaleString());
sleep (60000);
}
And I can see it stops after some time in the console logs.
Here it stopped at 10:39:03PM around 1 hour after 1 locked the screen and resumed when I unlocked the screen in the morning):
151.c4d77d78b4f4b23d.js:1 while background log 98
151.c4d77d78b4f4b23d.js:1 5/12/2024, 10:32:03 PM
151.c4d77d78b4f4b23d.js:1 while background log 99
151.c4d77d78b4f4b23d.js:1 5/12/2024, 10:33:03 PM
151.c4d77d78b4f4b23d.js:1 while background log 100
151.c4d77d78b4f4b23d.js:1 5/12/2024, 10:34:03 PM
151.c4d77d78b4f4b23d.js:1 while background log 101
151.c4d77d78b4f4b23d.js:1 5/12/2024, 10:35:03 PM
151.c4d77d78b4f4b23d.js:1 while background log 102
151.c4d77d78b4f4b23d.js:1 5/12/2024, 10:36:03 PM
151.c4d77d78b4f4b23d.js:1 while background log 103
151.c4d77d78b4f4b23d.js:1 5/12/2024, 10:37:03 PM
151.c4d77d78b4f4b23d.js:1 while background log 104
151.c4d77d78b4f4b23d.js:1 5/12/2024, 10:38:03 PM
151.c4d77d78b4f4b23d.js:1 while background log 105
151.c4d77d78b4f4b23d.js:1 5/12/2024, 10:39:03 PM
151.c4d77d78b4f4b23d.js:1 while background log 106
151.c4d77d78b4f4b23d.js:1 5/13/2024, 7:30:51 AM
Is there an explanation to this?
Another point to notice is that this behavior is not happening when I leave the developer tools panel open, the Web Worker does not get paused.
Is it driven by the browser only? Do we have some controls to prevent it to pause?
Xavier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.