Photo by Mohammad Rahmani on Unsplash
事件函式說明
變數 |
說明 |
Emitter.on() |
綁定事件 |
Emitter.emit() |
觸發事件 |
使用事件
// 載入事件
const EventEmitter = require('events');
class Emitter extends EventEmitter {};
const myEmitter = new Emitter();
// 建立自訂事件
myEmitter.on('my-event', (message) => {
console.log(`My event message: ${message}`);
})
setInterval(() => {
// 驅動事件
myEmitter.emit('my-event', 'Custom Log Message ' + new Date());
}, 3000);
參考資料
Donate KJ 贊助作者喝咖啡
如果這篇文章對你有幫助的話,可以透過下面支付方式贊助作者喝咖啡,如果有什麼建議或想說的話可以贊助並留言給我
If this article has been helpful to you, you can support the author by treating them to a coffee through the payment options below. If you have any suggestions or comments, feel free to sponsor and leave a message for me!