Do not use setInterval
Created by: sergeche
This PR replaces all occurrences of setInterval()
with self-resuming setTimeout()
.
The problem with setInterval()
is that browser schedules all handler invocations for a period of time while interval is active. Which leads to intensive CPU usage when browser or tab goes back from idle state.
Steps to reproduce
- Open any DASH sample, for example
samples/getting-started/auto-load-single-video-src.html
and pause video. - Put computer on sleep mode for a while (for example, 10 minutes)
- Wake up computer: you’ll see that example page hags up for a while and computer fans are spinning.
It happens because browser schedules setInterval()
callbacks for a period of active time. For example, if you set setInterval(fn, 100)
and 1 second passes, it must call fn
10 times (1000 / 100). Even if a main thread was active for a 300ms, it still schedules 10 fn
calls for 1 second while timer is active.
The same happens when you put computer asleep or browser tab becomes background: browser will immediately call all scheduled invocations for a period of time when tab was inactive but setInterval()
active.
To overcome this problem, a self-resuming setTimeout()
technique is used: schedule one fn
invocation and schedule next one only if timer was called.