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.

Revisions

  1. tuxracer created this gist Feb 16, 2014.
    33 changes: 33 additions & 0 deletions stop-timers.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    # 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