From fe499c2d38f8de8d3d290b76115f66506cc897b8 Mon Sep 17 00:00:00 2001 From: Orslumen Date: Fri, 8 Apr 2011 15:19:53 +0200 Subject: [PATCH] https://github.com/envjs/env-js/issues#issue/22: Call function synchronously when timeout is set to zero --- envjs/timer.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/envjs/timer.js b/envjs/timer.js index a7c6fb7e..ef7669ab 100644 --- a/envjs/timer.js +++ b/envjs/timer.js @@ -51,17 +51,22 @@ Envjs.once('tick', function(){ * @param {Object} time */ exports.setTimeout = setTimeout = function(fn, time){ - log.debug('setTimeout %s', time); - return Envjs.timers.addTimerOrInterval(fn, time, 'timeout'); + log.debug('setTimeout %s', time); + if (time === 0) { + fn.apply(fn,[]); + return -1; + } else { + return Envjs.timers.addTimerOrInterval(fn, time, 'timeout'); + } }; /** * clearTimeout * @param {Object} id */ -exports.clearTimeout = clearTimeout = function(id){ - log.debug('clearTimeout %s', id); - return Envjs.timers.removeTimerOrInterval(id, 'timeout'); +exports.clearTimeout = clearTimeout = function(id){ + log.debug('clearTimeout %s', id); + return id === -1 ? null : Envjs.timers.removeTimerOrInterval(id, 'timeout'); }; /**