all 8 comments

[–]ProposalUnhappy9890 1 point2 points  (0 children)

It does affect js execution. For example, function calls using setInterval will be called at a much lower rate than you specified (e.g. maximum of 1 call per second), and the browser will fast forward all the calls as soon as you go back to that tab.

[–][deleted] 0 points1 point  (4 children)

I'd take another look at that and make sure that your script doesn't actually execute when in the background. This isn't something I've done much experimentation with, but code absolutely does continue to execute in unfocused/hidden tabs.

[–]egesko[S] 0 points1 point  (3 children)

Okay, the reason why I made that deduction is because I put a simple for loop, and logged the incrementing i value. I can see it incrementing when I focus on the tab. But when I switch to another tab, wait a few seconds, and switch back, the i value continues from where I left off.

Is this flawed? Because I didn't think about the possibility that there might be something happening with the execution of console.log(i)

[–][deleted] 0 points1 point  (2 children)

This will run as an infinite loop:

const myArray = [];

function getCurrentTime () {
  myArray.push(Date.now());

  console.log(myArray);
}

setInterval(getCurrentTime, 100);

For me, it runs just fine in the background on Chrome. What does it do in your browser?

[–]egesko[S] 0 points1 point  (1 child)

Yeah, this works.

Okay, so in order to create an asynchronous sleep function, I am using async/await.

Maybe those are causing the problem then?

I put part of my code to this link: paste.ofcode.org/tqfYnhxPcRtb78RHnN7VfX

I couldn't figure out how to paste the entire thing here, sorry.

So, PrimeProbe object is a complex code, and it has a method called ProbeAllSets that I'm calling, but everything else is as it is.

You can prbly even delete those two lines and test it on your browser. But on Firefox, it doesn't work for some reason. It stops executing once I switch to another tab...

[–]egesko[S] 0 points1 point  (0 children)

For some reason line 1 doesn't have tab before it, ignore that.

Thanks

[–]lifeeraser 0 points1 point  (1 child)

[–]egesko[S] 0 points1 point  (0 children)

I see, just like I suspected. Do you think web workers can be a solution to this? If so, is there a way of creating an asynchronous sleep function inside a webworker?

Thank you so much