0

For some reason the window.setInterval thing stops working after about 60 seconds if its window is not visible enough.

Does anybody know how I can make it continue until I tell it to stop?

Here is a simple demo. I run it in chrome a web site, it does not stop in the VS debug environment.

function pseudoBot2() {
  const startTime = new Date();
  startTimeMs = startTime.getTime();
  const funcId = setInterval(logIt, 1000);
}
function logIt(){
  const now = new Date();
  const elapsed = now.getTime() - startTimeMs;
  console.log("TX at " + Math.round(elapsed / 1000));
}

If you start it with the chrome debugger console on then minimise its window then maximise after about 2 minutes the console log contains something like

.
.
TX at 61
TX at 62
TX at 63
TX at 105
TX at 165
.
.
tadman
  • 208,517
  • 23
  • 234
  • 262
  • Browsers are aggressive about conserving resources in background tabs. The interval shouldn't *stop* though but get throttled. Maybe paused and resumed. Are you sure nothing like this happens? – VLAZ Aug 30 '23 at 15:01
  • @VLAZ It gets a bit throttled as you see by the log at 105 seconds. But then it stops completely. – George Keeling Aug 30 '23 at 15:05
  • Is there some way to turn off the throttling? – George Keeling Aug 30 '23 at 15:10
  • Does the interval resume when the browser tab is re-focused, or just die completely? – spender Aug 30 '23 at 15:11
  • It's probably a bad idea, but `setInterval` in a worker doesn't get throttled. So you [can do something like this](https://stackoverflow.com/a/51391172/14357) – spender Aug 30 '23 at 15:14
  • 2
    Why turn of the throttling? That sounds like [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you trying to achieve? – VLAZ Aug 30 '23 at 15:15
  • Note that it's easier (and faster) to just use `Date.now()` instead of `new Date().getTime()`. – Heretic Monkey Aug 30 '23 at 15:35
  • Clearly the moderator has never used wikipedia. It's not plagiarism if you attach the reference. – twalow Aug 30 '23 at 16:04

0 Answers0