Related articles
- P01: Node.js tutorial from a practical perspective
- P02: Basic node usage
- P03: Node built-in module PATH
- P04: Buffer for nodeAPI
- P05: Events of node built-in module
- P06: Node built-in module FS (1)
- P07: Node built-in module FS (2)
- P08: Node implements static server ~ create project
- P09: Node implements static server ~ Hello HTTP
- P10: Node implements static server ~ static file or folder reading
Event trigger
-
Events is a built-in module
-
All objects that can fire events are instances of the EventEmitter class.
-
Events are usually named as humped strings, but any valid JavaScript property key can also be used.
-
When an EventEmitter object fires an event, all functions bound to the event are called synchronously. Any values returned by the invoked listener are ignored and discarded.
-
Simple to use
// eventEmitter. On () is used to register listeners, and eventEmitter. Emit () is used to emit events const eventEmitter = require('events'MyEvent extends EventEmitter{} const myEventClick = new myEvent( Check monitoring myClick myEventClick.'myClick',()=> { console.log('Click')})setInterval(() => {// trigger myClick myeventclick.emit ('myClick')}, 1000), <! MyClick myClick myClick... -->Copy the code
-
Pass the parameter params
- Note that the first parameter of emit is the name of the function to be fired and the subsequent parameters are custom parameters
- In addition, pay attention to the order of parameter passing. The actual participating parameters are no longer described
Const EventEmitter = require('events'EventEmitter extends EventEmitter{} const ce = new myEvent( ce.on('error', (err, time) => {console.log(err) console.log(time)})'error', new Error('error111'/** * Error: error111 at Object.<anonymous> (E:\***\***\?), date.now ())) // emit the first argument is the name of the triggered function. $\api\04_events_param.js:15:18) ... at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3) 1574242863420 * */Copy the code
-
Trigger only once In actual projects, events can be triggered only once. You can use the once command to do this. The concept is similar to vUE and JQ’s once
const EventEmitter = require('events'MyEvent extends EventEmitter{} const myEventClick = new myEvent( myEventClick.once('myClick',()=> { console.log('Click')})setInterval(() => {// trigger myClick myeventclick.emit ('myClick')}, 1000), <! MyClick -->Copy the code
-
RemoveListener removeAllListeners are removed
removeListener
Removing a particular function requires passing the listening event along with the corresponding functionremoveAllListeners
To remove all functions, simply pass in the corresponding listening event
const EventEmitter = require('events') Class myEvent extends EventEmitter{} Const ce = new myEvent(function fn1() { console.log('fn1'} // Event 2function fn2() { console.log('fn2'} // The function ce. On ('click', fn1) ce.on('click', fn2) setInterval(() => {// emit myClick ce.emit('click')}, 500)setInterval(() => {myClick ce.removelistener ()'click'Ce.removealllisteners ('click'}, 1500) // If fn2 is not printed after a certain period of time, it indicates that the listener has been removed /** * fn1 * fn2 * fn1 * fn1 * fn1 * fn1 * fn1 * fn1 * fn1 * fn1 * fn1 * fn1 */Copy the code
close