@ -222,7 +222,7 @@ exports.setTimeout = function(callback, after) {
exports . clearTimeout = function ( timer ) {
exports . clearTimeout = function ( timer ) {
if ( timer && ( timer . ontimeout || timer . _ onTimeout ) ) {
if ( timer && ( timer . ontimeout || timer . _ onTimeout ) ) {
timer . ontimeout = timer . _ onTimeout = null ;
timer . ontimeout = timer . _ onTimeout = null ;
if ( timer instanceof Timer || timer instanceof Time out ) {
if ( timer instanceof Timeout ) {
timer . close ( ) ; // for after === 0
timer . close ( ) ; // for after === 0
} else {
} else {
exports . unenroll ( timer ) ;
exports . unenroll ( timer ) ;
@ -232,39 +232,52 @@ exports.clearTimeout = function(timer) {
exports . setInterval = function ( callback , repeat ) {
exports . setInterval = function ( callback , repeat ) {
var timer = new Timer ( ) ;
if ( process . domain ) timer . domain = process . domain ;
repeat *= 1 ; // coalesce to number or NaN
repeat *= 1 ; // coalesce to number or NaN
if ( ! ( repeat >= 1 && repeat <= TIMEOUT_MAX ) ) {
if ( ! ( repeat >= 1 && repeat <= TIMEOUT_MAX ) ) {
repeat = 1 ; // schedule on next tick, follows browser behaviour
repeat = 1 ; // schedule on next tick, follows browser behaviour
}
}
var timer = new Timeout ( repeat ) ;
var args = Array . prototype . slice . call ( arguments , 2 ) ;
var args = Array . prototype . slice . call ( arguments , 2 ) ;
timer . ontimeout = function ( ) {
timer . _ onTimeout = wrapper ;
callback . apply ( timer , args ) ;
timer . _ repeat = true ;
}
if ( process . domain ) timer . domain = process . domain ;
exports . active ( timer ) ;
timer . start ( repeat , repeat ) ;
return timer ;
return timer ;
function wrapper ( ) {
callback . apply ( this , args ) ;
// If callback called clearInterval().
if ( timer . _ repeat === false ) return ;
// If timer is unref'd (or was - it's permanently removed from the list.)
if ( this . _ handle ) {
this . _ handle . start ( repeat , 0 ) ;
} else {
timer . _ idleTimeout = repeat ;
exports . active ( timer ) ;
}
}
} ;
} ;
exports . clearInterval = function ( timer ) {
exports . clearInterval = function ( timer ) {
if ( timer instanceof Timer ) {
if ( timer && timer . _ repeat ) {
timer . ontimeout = null ;
timer . _ repeat = false ;
timer . close ( ) ;
clearTimeout ( timer ) ;
}
}
} ;
} ;
var Timeout = function ( after ) {
var Timeout = function ( after ) {
this . _ idleTimeout = after ;
this . _ idleTimeout = after ;
this . _ idlePrev = this ;
this . _ idlePrev = this ;
this . _ idleNext = this ;
this . _ idleNext = this ;
this . _ idleStart = null ;
this . _ idleStart = null ;
this . _ onTimeout = null ;
this . _ onTimeout = null ;
this . _ repeat = false ;
} ;
} ;
Timeout . prototype . unref = function ( ) {
Timeout . prototype . unref = function ( ) {