Node.js 定时器
官网说明 https://nodejs.org/api/timers.html
The timer module exposes a global API for scheduling functions to be called at some future period of time. Because the timer functions are globals, there is no need to call require(‘timers’) to use the API.
The timer functions within Node.js implement a similar API as the timers API provided by Web Browsers but use a different internal implementation that is built around the Node.js Event Loop.
定时器模块为将来一段时间周期性的函数调用,暴露了一些全局接口。因为定时器函数是全局的,因此它无需调用require(‘timers’)来引入该API。
包括Node.js的定时器函数都实现了一个和Web浏览器相似的API,但是它们有着不同的内部实现,Node.js 中的定时器是建立在事件循环机制下的。
timeout.ref()
When called, requests that the Node.js event loop not exit so long as the Timeout is active. Calling timeout.ref() multiple times will have no effect.
当它被调用了以后,要求只要该定时器处于活动状态,那么Node.js的事件循环就不会退出。如果定时器已被 ref 那么再次调用 ref 不会产生其它影响。
timeout.unref()
When called, the active Timeout object will not require the Node.js event loop to remain active. If there is no other activity keeping the event loop running, the process may exit before the Timeout object’s callback is invoked. Calling timeout.unref() multiple times will have no effect.
当它被调用了以后,存活的超时对象将不需要Node.js事件循环保持活动状态。如果那里没有任何活动来保持时间循环持续运行,那么在超时对象的回调函数被调用之前,进程就将会退出。如果定时器已被 unref,再次调用 unref 不会产生其它影响。
在 setTimeout 的情景中当您 unref 您会创建另一个定时器,并唤醒事件循环。创建太多这种定时器可能会影响事件循环的性能,慎用。
server.on('error', function() {
try {
//强制退出机制
var killTimer = setTimeout(function() {
process.exit(1);
}, 3000);
killTimer.unref();
server.close(function(){
//此时所有的连接均已自动关闭,Node.js会自动退出,不需要再调用process.exit(1); 来结束所有进程。如果没有调用unref的话,由于定时器处于活动状态,那么Node.js的事件循环就不会退出。
});
} catch(e) {
console.log('err', e.stack);
}
});
setImmediate(callback[, …arg])
调度在所有 I/O 事件回调之后、setTimeout 和 setInterval 之前“立即”执行 callback。返回一个可能被 clearImmediate() 用到的 immediateId。可选地,您还能给回调传入参数。
setImmediate(function(){
console.log('Func exec immediately');
});
setInterval(callback, delay[, …arg])
调度每隔 delay 毫秒执行一次的 callback。返回一个可能被 clearInterval() 用到的 intervalId。可选地,您还能给回调传入参数。
Node.js中的参数和C/C++中的一样,都是传递副本,所以如果这里将编号用参数传递,则修改后的参数在函数结束后其生命周期就会消亡,故只能用全局变量来存储。
var i = 0;
setInterval(function(){
console.log('Func exec intervally, ' + i);
i++;
}, 1000);
setTimeout(callback, delay[, …arg])
调度 delay 毫秒后的一次 callback 执行。返回一个可能被 clearTimeout() 用到的 timeoutId。可选地,您还能给回调传入参数。
请务必注意,您的回调有可能不会在准确的 delay 毫秒后被调用。Node.js 不保证回调被触发的精确时间和顺序。回调会在尽可能接近所指定时间上被调用。
function timeoutFunc(name) {
console.log(name + ' timeout');
}
setTimeout(timeoutFunc, 1000, 'Func');
clearImmediate(immediate)
停止一个 immediate 的触发。
var immediateObj = setImmediate(function() {
for(var i=0;i<1000;i++) {
console.log(i);
}
console.log('Func exec immediately');
});
clearImmediate(immediateObj);
clearInterval(timeout)
停止一个 interval 的触发。
var intervalObj = setInterval(function(){
console.log('Func exec intervally');
}, 1000);
clearInterval(intervalObj);
clearTimeout(timeout)
阻止一个 timeout 被触发。
var timeoutObj = setTimeout(function() {
console.log('timeout');
}, 1000);
clearTimeout(timeoutObj);
相关文章
- JS自己实现字符串加密和解密算法
- JS框架_(JQuery.js)Tooltip弹出式按钮插件
- 【nodejs原理&源码赏析(7)】【译】Node.js中的事件循环,定时器和process.nextTick
- Node.js之Console用法小结
- [Node.js] Pass command line arguments to node.js
- [Node.js] node-persist: localStorage on the server
- [Node.js]26. Level 5 : Route rendering
- [Node.js]22. Level 4: Dependency
- 用 webpack 构建 node 后端代码,使其支持 js 新特性并实现热重载
- Node.js is the perfect choice for real-time application development
- 135、JS和Android交互范例
- python的前后端分离(一):django+原生js实现get请求
- [Node.js] Availability and Zero-downtime Restarts
- [Node.js] Exporting Modules in Node
- [Node.js] Intro to Web Scraping with Node and X-ray
- [Whole Web, Node.js, PM2] Configuring PM2 for Node applications
- [Node.js] Level 5. Express
- 华为OD机试 - 插队(Java & JS & Python)
- js全屏切换效果demo效果示例(整理)
- 【华为云技术分享】如何快速实现鲲鹏弹性云服务器Node.js部署和高可用性?
- 动态设置js的属性
- node.js JS对象和JSON字符串之间的转换
- Node.js:Jest测试框架测试test代码
- Node.js:Array.prototype.sort数组排序的使用
- Node.js学习笔记(四)——NodeJS访问MongoDB与MySQL数据库
- 深入Node.js的模块加载机制,手写require函数
- 详解js闭包
- JS 高级(二)闭包、封装
- Node.js(二)node 核心模块 fs、HTTP模块、express框架
- IT:前端进阶技术路线图(初级→中级→高级)之初级(研发工具/HTML/CSS/JS/浏览器)/中级(研发链路/工程化/库/框架/性能优化/工作原理)/高级(搭建/中后台/体验管理等)之详细攻略
- Node.js学习笔记——Express.js