Aaron
Aaron

Reputation: 1583

setInterval returns Timer Object and not intervalId

I want to clear an interval by using its intervalId which I intend to store in a local file.

I was under the impression that assigning setInterval returns its intervalId but I seem to be getting [object Timer] instead.


var fs = require("fs");

var id = setInterval(function(){
  console.log("tick");
}, 1000);

console.log(id);

var stream = fs.createWriteStream("id");
stream.once('open', function(fd) {
  stream.write(id);
});

I am using node v4.9

Upvotes: 8

Views: 4909

Answers (1)

chovy
chovy

Reputation: 75686

It is a timer object, but clears the way you would expect it to:

$ node ./test.js

var id = setInterval(function(){
  console.log("FOO");
  console.log(id);
}, 500);

setTimeout(function(){
  clearInterval(id);
}, 5000);

Outputs the following for 5 seconds, and clears as expected:

FOO
{ repeat: 0, callback: [Function] }
FOO
{ repeat: 0, callback: [Function] }
FOO
{ repeat: 0, callback: [Function] }

More info: http://nodejs.org/docs/v0.5.10/api/timers.html

Upvotes: 3

Related Questions