setInterval返回Timer对象而不是intervalId
我想通过使用我打算存储在本地文件中的intervalId来清除间隔。
我的印象是分配setInterval返回它的intervalId
但我似乎正在得到[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); });
我正在使用节点v4.9
这是一个计时器对象,但清除你期望的方式:
$ node ./test.js
var id = setInterval(function(){ console.log("FOO"); console.log(id); }, 500); setTimeout(function(){ clearInterval(id); }, 5000);
输出以下内容5秒,并按预期清除:
FOO { repeat: 0, callback: [Function] } FOO { repeat: 0, callback: [Function] } FOO { repeat: 0, callback: [Function] }
更多信息: http : //nodejs.org/docs/v0.5.10/api/timers.html