Browse Source

Allow optional params to setTimeout, setInterval

v0.7.4-release
Micheil Smith 15 years ago
committed by Ryan Dahl
parent
commit
6e3d12f617
  1. 4
      doc/api.txt
  2. 16
      src/node.js
  3. 32
      test/mjsunit/test-timers.js

4
doc/api.txt

@ -488,6 +488,8 @@ puts("The area of a cirlce of radius 4 is " + area(4));
To schedule execution of callback after delay milliseconds. Returns a To schedule execution of callback after delay milliseconds. Returns a
+timeoutId+ for possible use with +clearTimeout()+. +timeoutId+ for possible use with +clearTimeout()+.
Optionally, you can also pass arguments to the callback.
+clearTimeout(timeoutId)+:: +clearTimeout(timeoutId)+::
Prevents said timeout from triggering. Prevents said timeout from triggering.
@ -497,6 +499,8 @@ Prevents said timeout from triggering.
To schedule the repeated execution of callback every +delay+ milliseconds. Returns To schedule the repeated execution of callback every +delay+ milliseconds. Returns
a +intervalId+ for possible use with +clearInterval()+. a +intervalId+ for possible use with +clearInterval()+.
Optionally, you can also pass arguments to the callback.
+clearInterval(intervalId)+:: +clearInterval(intervalId)+::
Stops a interval from triggering. Stops a interval from triggering.

16
src/node.js

@ -491,17 +491,29 @@ process.Stats.prototype.isSocket = function () {
// Timers // Timers
function addTimerListener (callback) {
var timer = this;
// Special case the no param case to avoid the extra object creation.
if (arguments.length > 2) {
var args = Array.prototype.slice.call(arguments, 2);
timer.addListener("timeout", function(){
callback.apply(timer, args);
});
} else {
timer.addListener("timeout", callback);
}
}
GLOBAL.setTimeout = function (callback, after) { GLOBAL.setTimeout = function (callback, after) {
var timer = new process.Timer(); var timer = new process.Timer();
timer.addListener("timeout", callback); addTimerListener.apply(timer, arguments);
timer.start(after, 0); timer.start(after, 0);
return timer; return timer;
}; };
GLOBAL.setInterval = function (callback, repeat) { GLOBAL.setInterval = function (callback, repeat) {
var timer = new process.Timer(); var timer = new process.Timer();
timer.addListener("timeout", callback); addTimerListener.apply(timer, arguments);
timer.start(repeat, repeat); timer.start(repeat, repeat);
return timer; return timer;
}; };

32
test/mjsunit/test-timers.js

@ -40,6 +40,38 @@ setInterval(function () {
clearInterval(this); clearInterval(this);
}, 1000); }, 1000);
// Single param:
setTimeout(function(param){
assert.equal("test param", param);
}, 1000, "test param");
var interval_count2 = 0;
setInterval(function(param){
++interval_count2;
assert.equal("test param", param);
if(interval_count2 == 3)
clearInterval(this);
}, 1000, "test param");
// Multiple param
setTimeout(function(param1, param2){
assert.equal("param1", param1);
assert.equal("param2", param2);
}, 1000, "param1", "param2");
var interval_count3 = 0;
setInterval(function(param1, param2){
++interval_count3;
assert.equal("param1", param1);
assert.equal("param2", param2);
if(interval_count3 == 3)
clearInterval(this);
}, 1000, "param1", "param2");
process.addListener("exit", function () { process.addListener("exit", function () {
assert.equal(true, setTimeout_called); assert.equal(true, setTimeout_called);
assert.equal(3, interval_count); assert.equal(3, interval_count);

Loading…
Cancel
Save