diff --git a/lib/timers.js b/lib/timers.js index 3039b49f2c37..e25052ebb8b3 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -229,7 +229,7 @@ exports.setTimeout = function(callback, after) { timer = new Timeout(after); if (arguments.length <= 2) { - timer._onTimeout = callback; + timer._onTimeout = callback.bind(global); } else { /* * Sometimes setTimeout is called with arguments, EG @@ -241,9 +241,7 @@ exports.setTimeout = function(callback, after) { * desired in the normal case. */ var args = Array.prototype.slice.call(arguments, 2); - timer._onTimeout = function() { - callback.apply(timer, args); - } + timer._onTimeout = callback.bind(global, args); } if (process.domain) timer.domain = process.domain; @@ -284,7 +282,7 @@ exports.setInterval = function(callback, repeat) { return timer; function wrapper() { - callback.apply(this, args); + callback.apply(global, args); // If callback called clearInterval(). if (timer._repeat === false) return; // If timer is unref'd (or was - it's permanently removed from the list.)