Node. js event queue

Source: Internet
Author: User
When all asynchronous IO operations of Node. js are completed, an event is sent to the event queue. Node. js EventEmitter event queue

When all asynchronous I/O operations of Node. js are completed, an event is sent to the event queue.

The following describes the methods and instances of event queue.

Method:

[Code] 1. addListener (event, listener) adds a listener to the end of the listener array for the specified event. 2. on (event, listener) registers a listener for the specified event and accepts a string event and a callback function. 3. once (event, listener) registers a single listener for a specified event. That is, the listener can only be triggered once at most, and the listener is immediately released after being triggered. 4. removeListener (event, listener) removes a listener of a specified event. The listener must be a listener that has been registered for this event. 5. removeAllListeners ([event]) removes all listeners of all events. If an event is specified, all listeners of the specified event are removed. 6. setMaxListeners (n) by default, if you add more than 10 listeners, The EventEmitters will output warning information. The setMaxListeners function is used to increase the default number of listeners. 7. listeners (event) returns the listener array of the specified event. 8. emit (event, [arg1], [arg2], [...]) executes each listener in the order of parameters. If the event has a registered listener, true is returned. Otherwise, false is returned.

Class Method

[Code] 1. listenerCount (emitter, event) returns the number of listeners for the specified event.

Event

[Code] 1. newListener event-string, event name listener-event processing function this event is triggered when a new listener is added 2. removeListener event-string, the event name listener-the event handler function deletes a listener from the specified listener array. Note that this operation will change the indexes of those listeners after the listener is deleted.


Case 1: events are triggered every 1 s

Event1.js
[Code] // introduce the events module var events = require ('events'); // create the EventEmitter object var eventEmitter = new events. eventEmitter (); // registers the listener eventEmitter. on ('some _ event', function () {console. log ('some _ event trigger ');}); // timer setInterval (function () {// trigger some_event listener eventEmitter. emit ('some _ event');}, 1000 );

Event2.js

[code]var events = require('events');var eventEmitter = new events.EventEmitter();eventEmitter.on('listener', function (arg1, arg2) {    console.log('listener1', arg1, arg2);})eventEmitter.on('listener', function (arg1, arg2) {    console.log('listener2', arg1, arg2);});eventEmitter.emit('listener', 'zhang', 'li');

Case 3: register two Event Handlers for the same listener

[Code] var events = require ('events'); var eventEmitter = new events. eventEmitter (); var listener1 = function (arg1, arg2) {console. log ('listener1', arg1, arg2) ;}; var listener2 = function (arg1, arg2) {console. log ('listener2', arg1, arg2) ;}; // registers the listener eventEmitter. on ('listener ', listener1); eventEmitter. on ('listener ', listener2); // triggers the listener event // eventEmitter. emit ('listener ', 'zhang', 'lil'); var listenerLength = require ('events '). eventEmitter. listenerCount (eventEmitter, 'listener '); eventEmitter. emit ('listener ', 'zhang', 'lil'); console. log ('listener listener registration' + listenerLength + 'event processing functions ');

The above is the content of the Node. js event queue. For more information, see PHP Chinese Network (www.php1.cn )!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.