Home  >  Article  >  WeChat Applet  >  Introduction to methods of using event listeners in mini program development

Introduction to methods of using event listeners in mini program development

青灯夜游
青灯夜游forward
2020-04-24 09:22:113809browse

Introduction to methods of using event listeners in mini program development

Event monitoring is often used in development. In programming languages, a listener is a device specially used to monitor events or status changes that occur on other objects and handle them accordingly. Object, when something happens to the monitored object, take corresponding action immediately. The listener is characterized by passive triggering, and the execution of trigger events is immediate. There is also such an event handling mechanism in small program development. All asynchronous I/O operations in Node.js will send an event to the event queue when completed. Many objects in Node.js emit events: a net.Server object emits an event every time a new connection is made, and a fs.readStream object emits an event when a file is opened. All of these event-generating objects are instances of events.EventEmitter. SoHow to use event listeners in mini program production?

events module only provides one object: events.EventEmitter

The core of EventEmitter is the encapsulation of event triggering and event listener functions.

Each event of EventEmitter consists of an event name and several parameters. The event name is a string, which usually expresses certain semantics. For each event, EventEmitter supports several event listeners.

When an event is triggered, the event listeners registered to this event are called in sequence, and the event parameters are passed as callback function parameters.

//event.js 文件
var events =require(\'events\');
var emitter= new events.EventEmitter();
emitter.on(\'someEvent\',function(arg1, arg2) {
console.log(\'listener1\', arg1, arg2);
});
emitter.on(\'someEvent\',function(arg1, arg2) {
console.log(\'listener2\', arg1, arg2);
});
emitter.emit(\'someEvent\',\'arg1 参数\', \'arg2 参数\');

addListener(event, listener) Adds a listener for the specified event to the end of the listener array.

on(event,listener) registers a listener for the specified event, accepting a string event and a callback function.

once(event,listener) Register a one-time listener for the specified event, that is, the listener will only be triggered once at most, and the listener will be released immediately after being triggered.

Introduction to methods of using event listeners in mini program development

removeListener(event,listener) Removes a listener for the specified event. The listener must be a registered listener for the event.

It accepts two parameters, the first is the event name, and the second is the callback function name.

removeAllListeners([event]) removes all listeners for all events. If an event is specified, all listeners for the specified event are removed.

setMaxListeners(n) By default, EventEmitters will output a warning message if you add more than 10 listeners. The setMaxListeners function is used to increase the default limit of the number of listeners.

listeners(event) returns the listener array for the specified event.

emit(event,[arg1], [arg2], […]) Execute each listener in the order of parameters. If the event has a registered listener, it returns true, otherwise it returns false.

ListenerCount(emitter,event) in the class method returns the number of listeners for the specified event.

Listening events

removeListener removes a listener from the specified listener array. It should be noted that this operation will change the index of those listeners after the deleted listener.

event - string, event name

listener - event processing function

newListener This event is triggered when a new listener is added.

event - string, event name

listener - event processing function

error It contains error semantics. We usually trigger the error event when we encounter an exception. .

When error is triggered, EventEmitter stipulates that if there is no responding listener, Node.js will treat it as an exception, exit the program and output an error message. We generally need to set up listeners for objects that trigger error events to prevent the entire program from crashing when an error occurs.

Most of the time we will not use EventEmitter directly, but inherit it in the object. Including fs, net, http, as long as the core modules that support event response are subclasses of EventEmitter. The advantage of this is that the event implementation of an object with a certain entity function conforms to the semantics, and the monitoring and emission of events should be methods of an object. Secondly, the object mechanism of js is based on prototype and supports partial multiple inheritance. Inheriting EventEmitter will not disrupt the original inheritance relationship of the object.

Recommendation: " Mini Program Development Tutorial"

The above is the detailed content of Introduction to methods of using event listeners in mini program development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jisuapp.cn. If there is any infringement, please contact admin@php.cn delete