timers模块
var timers = require('timers'); function A() {
//将A对象注册到定时器里
timers.enroll(this, );
//进行激活,如果不激活,该对象的onTimeout方法不会被调用,每调用一次,将_idleTimeout的值重置
timers._unrefActive(this);
} A.prototype.foo = function () {
//将_idleTimeout的值重置
timers._unrefActive(this);
} A.prototype.bar = function () {
//取消注册
timers.unenroll(this);
} A.prototype._onTimeout = function () {
console.log("connect timeout"); }; module.exports = exports = A;
var A = require('./abc.js'); var test = new A(); //定时器,每间隔一秒钟从1输出到10
(function () {
var num = ;
var timer = setInterval(function () {
num++;
if(num == ){
//重置了_idleTimeout,所以是在5秒时触发
test.foo();
}
console.log(num);
if (num >= ) {
clearTimeout(timer);
return;
}
}, );
})();