JS timing out

Somewhat clueless here. It seems that if I leave the browser window open, eventually my JavaScript seems to break. I am not sure if the browser is “timing out” but it seems to forget what it already knows e.g. my variables etc. After this “time out” happens, when interacting with the DOM again the browser can’t successfully carry out my JavaScript functions correctly or event at all.

Should I add a fake click event? If so, how often? Is there a better solution? Is there a way to identify how long before the fake event should be triggered?

What I don’t like about this fake event trigger is that it depends on the browsers memory and will be constantly running counting down the milliseconds. I don’t like having a settimeout function in place constantly.

Some context is needed here @cssissimple, what Javascript? If you can share the code, maybe someone can help you :slight_smile:

Also, what browser? None of the major browsers should do anything like this.

Don’t some browsers pause js in inactive tabs? Could that affect setInterval and setTimeout etc?

It could affect timers, but it wouldnt erase the engine’s variable table.

It kind of sounds like the computer is having a memory collapse, which would point towards a hardware problem, but…

1 Like

Yes, the browser is inactive.

Sorry.

Yes, if I leave the window open for an extended period of time and do not interact with the DOM, then this issue occurs.

Browsers sometimes clear memory after idle time. Have you considered using localStorage or session management to preserve state instead of a fake event?

I don’t want to use local storage.

Is there another option you are aware of?

Does anyone know of a viable option to resolving the browser timing out or pausing for inactive windows?

If it affected the timers then what would a solution be? Settimeout wouldn’t work if the timer itself stopped working when a tab becomes inactive. I couldn’t even trigger a fake event at that point.

Do you know at what point does a browser window becomes inactive? As in how long? Still trying to figure this out.

I am sure, that we are on a complete wrong way here. There is nothing that removes variables or js code from the DOM when the tab is inactive. All my apps are open for hours in a tab and only used on demand a few times e day but they work perfectly.

So there is something different causing the TO issue.

4 Likes

Another option you could try is using in-memory state management with periodic checks for DOM visibility (like document.hidden). If you combine that with restoring state when the user returns focus, it might give you a lighter, cleaner alternative without running timers nonstop.