ifvisible.js
ifvisible.js copied to clipboard
Memory leak with idle checking
When idle checking is on, every mouse move, keystroke, and scroll event calls wake
which iterates through an array of old timers and clears them (though only effectively clearing the last timer which would be the only one ever active). This array grows unbounded with each event. An array should not be used here.
Current:
trackIdleStatus = ->
timer = []
wakeUp = ->
timer.map(clearTimeout);
ifvisible.wakeup() if status isnt "active"
idleStartedTime = +(new Date())
timer.push setTimeout(->
ifvisible.idle() if status is "active"
, idleTime)
...
Fix:
trackIdleStatus = ->
timer = null # No array, just a reference to the last timeout id
wakeUp = ->
clearTimeout(timer); # Clear previous timeout. A noop if the timeout already ran
ifvisible.wakeup() if status isnt "active"
idleStartedTime = +(new Date())
timer = setTimeout(->
ifvisible.idle() if status is "active"
, idleTime)
...
When page is idle and mousemove to 'wake up', ifvisible.wakeUp(wakeUp) and 'mousemove' clear the same timer and set 2 timer. So one of the timers won't be cleared and it will always trigger 'idle' callback. That's what i found in v1.0.6. :(
@AbelMakihara good catch. Here is an updated fix:
Fix:
trackIdleStatus = ->
timer = null # No array, just a reference to the last timeout id
wakeUp = ->
return ifvisible.wakeup() if status isnt "active"
clearTimeout(timer); # Clear previous timeout. A noop if the timeout already ran
idleStartedTime = +(new Date())
timer = setTimeout(->
ifvisible.idle() if status is "active"
, idleTime)
...
is still on maintain ?
I do believe this to be fixed in my fork https://github.com/rosskevin/ifvisible. If not please PR a test (expanded tests are present there)