Created
March 1, 2013 14:11
-
-
Save iamandycohen/5064893 to your computer and use it in GitHub Desktop.
JavaScript Timer
This file contains 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
Timer = function (timerFn, delay, intervalFn, interval) { | |
/// <summary>Set a timer/timeout with Stop, Start, and Restart capability</summary> | |
/// <param name="timerFn" type="Function">Function to run after the amount of time specified by the delay parameter</param> | |
/// <param name="delay" type="Number">Amount of time to delay the timerFn</param> | |
/// <param name="intervalFn" type="Function">Function to run after the interval specified in the interval parameter</param> | |
/// <param name="interval" type="Number">Amount of time to run the intervalFn function (frequency)</param> | |
var _self = this, | |
_timerFn = timerFn, | |
_delay = delay, | |
_intervalFn = intervalFn, | |
_interval = interval, | |
_timeoutID = null, | |
_intervaID = null; | |
_self.Start = function () { | |
/// <summary>Starts the Timer</summary> | |
_timeoutID = setTimeout(_timerFn, _delay); | |
if (_intervalFn !== undefined) { | |
_intervaID = setInterval(_intervalFn, _interval); | |
} | |
} | |
_self.Stop = function () { | |
/// <summary>Stops the Timer</summary> | |
clearTimeout(_timeoutID); | |
if (_intervaID !== null) { | |
clearInterval(_intervaID); | |
} | |
} | |
_self.Restart = function () { | |
/// <summary>Restarts the Timer</summary> | |
_self.Stop(); | |
_self.Start(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment