我看遍了所有中文资料的 unref 资料,发现全是错的。。也是醉了。
官方文档:https://nodejs.org/dist/latest-v5.x/docs/api/timers.html#timers_unref
来自:http://www.jb51.net/article/57861.htm
如果这个引用正确的话,那么《Node.js权威指南》这本书里的解释也是错的。
网上大部分的说法都说 unref 会暂停一个回调函数的调用,可实测的代码如下
var timer = setInterval(function () {
console.log(new Date, 1)
}, 1000)
var timer2 = setInterval(function () {
console.log(new Date, 2)
}, 1000)
timer2.unref()
timer2 始终没有被暂停。
按我对于文档的理解,unref 的意思只是让 timer 不会阻止 node 进程的退出,类似垃圾回收时候的引用计数-1的感觉。但不管是否调用了 unref,回调函数都会一直运行。
为了搜索引擎能过来,我堆点关键词:
javascript中setTimeout和setInterval的unref()和ref()
nodejs中的全局函数setTimeout/clearTimeout,setInterval/clearInterval,unref/ref