Skip to content

Instantly share code, notes, and snippets.

@tuxracer
Created February 16, 2014 00:20
Show Gist options
  • Save tuxracer/9027318 to your computer and use it in GitHub Desktop.
Save tuxracer/9027318 to your computer and use it in GitHub Desktop.
Stop timers on dispose of chaplin views, controllers, models, and collections
# 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