Sha256: f0e2bfaee430fcbeb16341e10e1af40328690b55551bfb7e2dbeb375a271e145

Contents?: true

Size: 1.78 KB

Versions: 30

Compression:

Stored size: 1.78 KB

Contents

// 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
   */
  
  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
   */

  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
   */

  clearInterval = clearTimeout = function(id) {
    return delete timers[--id]
  }
  
  /**
   * Reset timers.
   *
   * @return {array}
   * @api public
   */
  
  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]()
        }
  }
  
})()

Version data entries

30 entries across 30 versions & 4 rubygems

Version Path
bootcamp-0.1.4 vendor/test_suites/jspec/lib/jspec.timers.js
bootcamp-0.1.0 vendor/test_suites/jspec/lib/jspec.timers.js
uki-1.1.4 frameworks/jspec/lib/jspec.timers.js
uki-1.1.3 frameworks/jspec/lib/jspec.timers.js
uki-1.1.2 frameworks/jspec/lib/jspec.timers.js
jspec-4.3.2 lib/jspec.timers.js
jspec-steventux-3.3.2.1 lib/jspec.timers.js
jspec-steventux-3.3.2 lib/jspec.timers.js
jspec-4.3.1 lib/jspec.timers.js
jspec-4.3.0 lib/jspec.timers.js
uki-1.1.1 frameworks/jspec/lib/jspec.timers.js
uki-1.1.0 frameworks/jspec/lib/jspec.timers.js
jspec-4.2.1 lib/jspec.timers.js
jspec-4.2.0 lib/jspec.timers.js
uki-1.0.2 frameworks/jspec/lib/jspec.timers.js
uki-1.0.1 frameworks/jspec/lib/jspec.timers.js
jspec-4.1.0 lib/jspec.timers.js
jspec-4.0.0 lib/jspec.timers.js
uki-1.0.0 frameworks/jspec/lib/jspec.timers.js
jspec-3.3.3 lib/jspec.timers.js