Skip to content

Instantly share code, notes, and snippets.

@joliver
Created April 26, 2010 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joliver/379414 to your computer and use it in GitHub Desktop.
Save joliver/379414 to your computer and use it in GitHub Desktop.
lib/jspec.timers.js | 8 ++++----
1 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/lib/jspec.timers.js b/lib/jspec.timers.js
index c88d10b..82df7ad 100644
--- a/lib/jspec.timers.js
+++ b/lib/jspec.timers.js
@@ -24,7 +24,7 @@
* @api public
*/
- setTimeout = function(callback, ms) {
+ window.setTimeout = function(callback, ms) {
var id
return id = setInterval(function(){
callback()
@@ -41,7 +41,7 @@
* @api public
*/
- setInterval = function(callback, ms) {
+ window.setInterval = function(callback, ms) {
callback.step = ms, callback.current = callback.last = 0
return timers[timers.length] = callback, timers.length
}
@@ -54,7 +54,7 @@
* @api public
*/
- clearInterval = clearTimeout = function(id) {
+ window.clearInterval = window.clearTimeout = function(id) {
return delete timers[--id]
}
@@ -65,7 +65,7 @@
* @api public
*/
- resetTimers = function() {
+ window.resetTimers = function() {
return timers = []
}
// JSpec - Mock Timers - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
;(function(){
/**
* Version.
*/
mockTimersVersion = '1.0.2'
/**
* Localized timer stack.
*/
var timers = []
/**
* Set mock timeout with _callback_ and timeout of _ms_.
*
* @param {function} callback
* @param {int} ms
* @return {int}
* @api public
*/
window.setTimeout = function(callback, ms) {
var id
return id = setInterval(function(){
callback()
clearInterval(id)
}, ms)
}
/**
* Set mock interval with _callback_ and interval of _ms_.
*
* @param {function} callback
* @param {int} ms
* @return {int}
* @api public
*/
window.setInterval = function(callback, ms) {
callback.step = ms, callback.current = callback.last = 0
return timers[timers.length] = callback, timers.length
}
/**
* Destroy timer with _id_.
*
* @param {int} id
* @return {bool}
* @api public
*/
window.clearInterval = window.clearTimeout = function(id) {
return delete timers[--id]
}
/**
* Reset timers.
*
* @return {array}
* @api public
*/
window.resetTimers = function() {
return timers = []
}
/**
* Increment each timers internal clock by _ms_.
*
* @param {int} ms
* @api public
*/
tick = function(ms) {
for (var i = 0, len = timers.length; i < len; ++i)
if (timers[i] && (timers[i].current += ms))
if (timers[i].current - timers[i].last >= timers[i].step) {
var times = Math.floor((timers[i].current - timers[i].last) / timers[i].step)
var remainder = (timers[i].current - timers[i].last) % timers[i].step
timers[i].last = timers[i].current - remainder
while (times-- && timers[i]) timers[i]()
}
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment