Search code examples
javascriptgoogle-chrometimersettimeoutweb-worker

Chrome Web Workers Stop Running


I am running a setTimeout timer to update our JWT token. Most of the time it works fine, but it appears Chrome is throttling the setTimeout. After doing some research I changed it to use hacktimer and moved it to a Web Worker, which appears to be slightly more reliable, but still causes the setTimeout to stop for a period of 30 minutes, causing the token to be invalid. And yes, I verified that hacktimer is working. Is there another solution to dealing with the throttling on Chrome based browsers?


Solution

  • Below is a snippet that plays audio constantly in a loop, click the play button and it starts an interval that counts, this is compared to how many seconds have ellapsed, I tested going to other tabs its seems to keep the interval & time in sync, so it doesn't seem to throttle it. I waited a fair amount of time, not 30 mins, maybe give this a try.

    const qs = s => document.querySelector(s);
    
    const btn = qs("button");
    const audio = qs("audio");
    const timeoutTicks = qs('#timeoutTicks');
    const fromStart = qs("#fromStart");
    
    btn.addEventListener('click', () => {
      audio.play();
      let counter = 0;
      const started = Date.now();
      setInterval(() => {
         timeoutTicks.innerText = ++counter;
         fromStart.innerText = Math.round((Date.now() - started)/1000);
      }, 1000); 
    });
    .tick {
      display: grid;
      grid-template-columns: auto 1fr;
      gap: 1em;
      padding: 1em;
      background-color: black;
      color: white;
    }
      
    <div class="tick">
      <span>Set Timeout Ticks</span>
      <span id="timeoutTicks">0</span>
      <span>Seconds from start</span>
      <span id="fromStart">0</span>  
      <button>start</button>
    </div>
    
    <audio
      paused
       loop
       controls
       src="https://upload.wikimedia.org/wikipedia/commons/d/de/Lorem_ipsum.ogg"/>