SetInterval returns a Timer object, not an intervalId

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

I got the impression that setting setInterval returns its intervalId , but I seem to get [object Timer] .


 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

+6
source share
1 answer

This is a timer object, but clears it as you expected:

$ node. / test.js

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

Displays the following data for 5 seconds and clears as expected:

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

Additional information: http://nodejs.org/docs/v0.5.10/api/timers.html

+2
source

Source: https://habr.com/ru/post/900304/


All Articles