Created
February 16, 2014 00:20
-
-
Save tuxracer/9027318 to your computer and use it in GitHub Desktop.
Stop timers on dispose of chaplin views, controllers, models, and collections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using @setTimeout or @setInterval will clear the timers on dispose | |
# Mixin, can be used with https://github.com/HubSpot/mixen | |
start = (type, args) -> | |
unless @disposed | |
method = window["set#{type}"] | |
timer = method.apply window, args | |
@timers[type].push timer | |
timer | |
module.exports = class StopTimers | |
constructor: -> | |
@resetTimers() | |
super | |
resetTimers: -> | |
@timers = | |
Interval: [] | |
Timeout: [] | |
setInterval: -> | |
start.call @, 'Interval', arguments | |
setTimeout: -> | |
start.call @, 'Timeout', arguments | |
dispose: -> | |
for key, val of @timers | |
clearMethod = window["clear#{key}"] | |
val.forEach clearMethod | |
@resetTimers() | |
super |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment