猿问

NodeJS eventEmitter回调函数内如何突破自触发次数限制

const EventEmitter = require('events')

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter()

let times = 0

myEmitter.on('canReceive', function () {

    console.log(++times)

    myEmitter.emit('canReceive')

})

myEmitter.emit('canReceive')

为何栈溢出了,有什么方式可以一直触发但是防止溢出吗?

海绵宝宝撒
浏览 685回答 2
2回答

蓝山帝景

给他一点时间myEmitter.on('canReceive', function () {    console.log(++times)    setTimeout(function () => {        myEmitter.emit('canReceive')    }, 0)})

郎朗坤

on...本来就是等待触发时机的~
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答