Having async issues with setInterval and other timing

I have a program that relies on setInterval for repeating 3 basic tasks indefinitely, in a specific order. Everything works fine until I switch tabs in the browser. Once I switch to a different tab, the timing goes haywire, things fire out of order, and my clearInterval functions don’t even stop the interval correctly.

I’m fairly new to JS, and I only have a high level understanding of the DOM and Event Loop, I’m assuming that is where my problem is. I’m not even sure where to start debugging this one. The code base is very large and proprietary, I’m not going to be able to share any actual code.

If they rely on a specific order, why not have each function trigger the next event?

Instead of having three independent setIntervals (which is what I’m assuming is what you’re doing), You start your program by triggering Function A, which does its thing then waits 1 second (or whatever your interval is), then triggers Function B. Function B then does its thing then waits another 1 second, then triggers Function C. Function C then does its thing, waits 1 second, then triggers Function A.

I’m not sure about that but maybe a loop with timeout promise at the end instead of an interval will work better?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.