Node.js – 处理“退出”事件
由于以下原因,当进程要退出时会发出“exit”事件-
Process.exit()方法被显式调用。
节点事件循环不再有任何任务要执行。
语法
Event: 'exit'
示例1
创建一个文件“exit.js”并复制以下代码片段。创建文件后,使用命令“nodeexit.js”运行此代码。
//处理“退出”事件演示示例
console.log("Process Starts")
//将此事件绑定到处理程序
process.on('exit',() => {
console.log("process.exit() method is called")
})
console.log("Process Ends")
//退出进程
process.exit()输出结果Process Starts Process Ends process.exit() method is called
示例2
让我们再看一个例子。
//Proxess'Exit'事件演示示例
//导入事件模块
const events = require("events")
console.log("进程开始...")
const eventEmitter = new events.EventEmitter()
//初始化事件处理程序
var Handler = function() {
//调用退出事件
process.on('exit', () => {
console.log("process.exit() method is called")
})
}
//调用hello事件
eventEmitter.on("hello", Handler)
//发出事件
eventEmitter.emit("hello")
console.log("进程结束...")
//退出进程
process.exit()输出结果进程开始... 进程结束... process.exit() method is called
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短