Nodejs event monitoring and trigger understanding analysis, nodejs events

Source: Internet
Author: User

Nodejs event monitoring and trigger understanding analysis, nodejs events

This article analyzes the monitoring and triggering of node. js events. Share it with you for your reference. The specific analysis is as follows:

Regarding the event-driven node. JS, I still did not understand it after reading "deep dive into node. js" (it may be a bit deep, or I cannot understand it well ), today, I saw an article about monitoring and triggering node. js events in the Turing community. The example is easy to understand, so I also understand the node. js event driver.

The following references the Turing community article (Address: http://www.ituring.com.cn/article/177478)

First, let's take a look at the Event module of nodejs:

Most modules in Node. js are inherited from the Event module. The Event module (events. EventEmitter) is an implementation class in simple Event listener mode. Its objects include addListener, on, once, removeListener, removeAllListeners, emit, and other basic event listening modes.

First, let's look at an example:

Var events = require ("events"); var emitter = new events. eventEmitter (); // creates an object for the event listener // listens to the event some_eventemitter.on ("some_event", function () {console. log ("event triggered, call this callback function") ;}); setTimeout (function () {emitter. emit ("some_event"); // trigger event some_event}, 3000 );

Seeing this example reminds me of jQuery's custom events:

// Bind the hello event element. on ("hello", function () {alert ("hello world! ") ;}); // Triggers the hello event element. trigger (" hello ");

In this case, it is easy to understand the monitoring and triggering of node. js events. Emit is equivalent to trigger events in jQuery.

I hope this article will help you design nodejs programs.

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.