Understanding and analysis of monitoring and triggering of Nodejs events _node.js

Source: Internet
Author: User
Tags emit event listener

This paper analyzes the monitoring and triggering of NODEJS events. Share to everyone for your reference. The specific analysis is as follows:

About Nodejs event-driven, read the "Nodejs" or did not understand (may write a bit deep, or their own understanding is not good enough), today in the Turing community to see a Nodejs event on the monitoring and triggering, because given the example of more people, it is easy to understand, Therefore, it is also understood that the Nodejs event-driven.

The following is a reference to the Turing Community article (address: http://www.ituring.com.cn/article/177478)

First, take a look at the Nodejs event module:

Most of the modules in the Node.js are inherited from the event module. The event module (Events.eventemitter) is an implementation class for a simple incident listener pattern. Its object has the addlistener,on,once,removelistener,removealllisteners,emit and so on the basic event listener pattern realization method.

First look at an example:

var events = require ("events");
var emitter = new events. An object
//Listener event Some_event
emitter.on ("Some_event", function () {
  console.log ("Eventemitter") that created the event listener. Event Trigger, call this callback function ");
settimeout (function () {
  emitter.emit ("some_event");   Triggering event some_event
},3000);

Seeing this example reminds me of jquery's Custom events:

To bind the element Hello event
element.on ("Hello", function () {
  alert ("Hello world!");
});
Trigger Hello event
element.trigger ("Hello");

Such a comparison makes it easy to understand the monitoring and triggering of NODEJS events. Emit is equivalent to the trigger trigger event in JQuery.

I hope this article will help you with the NODEJS program design.

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.