08 Mu Class Network "Attack on node. JS Foundation (i)" Events

Source: Internet
Author: User
Tags emit

Referencing the Eventemitter in the events module

Monitoring and launching of events

The same number of event launches is limited, and peaks can be set via Setmaxlisteners

var Eventemitter = require (' Events '). Eventemittervarnew  eventemitter ()// plus no warning //  Life.setmaxlisteners(life.on) (' clickevent ',function(WHO) {    Console.log ( ' give ' + who + ' pour water ')}) Life.emit (' clickevent ', ' Man 1 ')

Each emit has a return value indicating whether there is a corresponding listener event
var hasclicklistener = life.emit (' clickevent ', ' Man ')var haschangelistener = Life.emit (' ChangeEvent ', ' sister 1 ') console.log (Hasclicklistener) console.log (Haschangelistener) console.log (Hasputlistener) 
Cancel Monitoring
//can be placed behind on and emit .functionWater (WHO) {Console.log (' Give ' + who + ' pour water ')}life.on (' ChangeEvent ', water)//Has not been canceled, emit cannot be put on beforeLife.emit (' ChangeEvent ', ' Zhang San ')//water can not use anonymous functions, RemoveListener need to be before emitLife.removelistener (' ChangeEvent ', water)//Remove all listeners, you can pass event names as parameters//life.removealllisteners ()varHaschangelistener = Life.emit (' changeevent ', ' sister 1 ')//The listener was canceled .varHasputlistener = Life.emit (' clickevent ', ' Li Bai ')////Whether it is monitoredConsole.log (Haschangelistener) console.log (Hasputlistener)

Number of listeners
Console.log (' changeevent: ' + life.listeners (' ChangeEvent '). Length) Console.log (' clickevent: ' + Life.listeners (' clickevent '). Length) Console.log (' all: ' + life.listeners.length)//  Eventemitter provides the method Console.log (' Eventemitter provides: ') console.log (' changeevent: ' + Eventemitter.listenercount (Life, ' ChangeEvent ')) Console.log (' clickevent: ' + eventemitter.listenercount ( Life, ' clickevent '))

08 Mu Class Network "Attack on node. JS Foundation (i)" Events

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.