nodejs 事件机制
setTimeout(myFunc,1000);
var myTimeout=setTimeout(myFunc,1000);
...
clearTimeOut(myTimeout);
var myInterval=setInterval(myFunc,1000);
...
clearInterval(myInterval);
var myImmediate=setImmediate(myFunc,1000);
...
clearImmediate(myImmediate);
二 事件发射器和监听器
var EventEmitter = require('events').EventEmitter; // 引入事件模块
var event = new EventEmitter(); // 实例化事件模块
// 注册事件(customer_event)
event.on('customer_event', function() {
console.log('customer_event has be occured : ' + new Date());
});
setInterval(function() {
event.emit('customer_event'); // 发射(触发)事件
}, 500);
var EventEmitter = require('events').EventEmitter; // 引入事件模块
var event = new EventEmitter(); // 实例化事件模块
// 注册事件(sayHello)
event.on('sayHello', function(param1, param2) {
console.log('Hello1 : ', param1, param2);
});
// 再次注册事件(sayHello)
event.on('sayHello', function(param1, param2) {
console.log('Hello2 : ', param1, param2);
});
event.emit('sayHello', 'GuYing', '1996'); // 发射(触发)事件(sayHello)
var events=require('events');
var http=require('http');
function UserBean(){
//实例化事件模型
this.eventEmit=new events.EventEmitter();
this.zhuce=function(req,res){
console.log('注册');
req['uname']='aa';
req['pwd']='bb';
//触发事件
this.eventEmit.emit('zhuceSuccess','aa','bb');
},
this.login=function(req,res){
console.log('登录');
res.write('用户名:'+req['uname']);
res.write('密码:'+req['pwd']);
res.write("登录");
}
}
module.exports=UserBean;
var http=require('http');
var UserBean=require('./UserBean');
http.createServer(function(request,response){
response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
if(request.url!=='favicon.ico'){
user=new UserBean();
user.eventEmit.once('zhuceSuccess',function(uname,pwd){
response.write('注册成功');
console.log('传uname '+uname);
console.log('传pwd '+pwd);
user.login(request,response);
response.end();
});
user.zhuce(request,response);
}
}).listen(8000);
console.log('server running at http://127.0.0.1:8000/');


var events=require('events');
var myEvent = new events.EventEmitter();
myEvent.emit('error', new Error('whoops!'));
var events=require('events');
myEvent.on('error', (err) => {
console.log('whoops! there was an error');
});
myEvent.emit('error', new Error('whoops!'));

function myObj(){
Events.EventEmitter.call(this);
}
myObj.prototype._proto_=evnets.EventEmitter.prototype;
var newObj=new myObj();
newObj.emit('someEvent');
var events = require('events');
function Account() {
this.balance = 0;
events.EventEmitter.call(this);
this.deposit = function(amount){
this.balance += amount;
this.emit('balanceChanged');
};
this.withdraw = function(amount){
this.balance -= amount;
this.emit('balanceChanged');
};
}
Account.prototype.__proto__ = events.EventEmitter.prototype;
function displayBalance(){
console.log("Account balance: $%d", this.balance);
}
function checkOverdraw(){
if (this.balance < 0){
console.log("Account overdrawn!!!");
}
}
function checkGoal(acc, goal){
if (acc.balance > goal){
console.log("Goal Achieved!!!");
}
}
var account = new Account();
account.on("balanceChanged", displayBalance);
account.on("balanceChanged", checkOverdraw);
account.on("balanceChanged", function(){
checkGoal(this, 1000);
});
account.deposit(220);
account.deposit(320);
account.deposit(600);
account.withdraw(1200);
nodejs 事件机制的更多相关文章
- nodejs事件机制
var EventEmitter = function() { this.evts = {}; }; EventEmitter.prototype = { constructor: EventEmit ...
- EventEmitter:nodeJs事件触发机制
Node.js 所有的异步 I/O 操作在完成时都会发送一个事件到事件队列 Node.js 里面的许多对象都会分发事件:一个 net.Server 对象会在每次有新连接时触发一个事件, 一个 fs.r ...
- 12.nodejs事件轮询机制
一:nodejs事件轮询机制 就是 函数的执行顺序 <script type="text/javascript"> setImmediate(function(){ ...
- Ext JS学习第十六天 事件机制event(一) DotNet进阶系列(持续更新) 第一节:.Net版基于WebSocket的聊天室样例 第十五节:深入理解async和await的作用及各种适用场景和用法 第十五节:深入理解async和await的作用及各种适用场景和用法 前端自动化准备和详细配置(NVM、NPM/CNPM、NodeJs、NRM、WebPack、Gulp/Grunt、G
code&monkey Ext JS学习第十六天 事件机制event(一) 此文用来记录学习笔记: 休息了好几天,从今天开始继续保持更新,鞭策自己学习 今天我们来说一说什么是事件,对于事件 ...
- nodeJS中的事件机制
events模块是node的核心模块,几乎所有常用的node模块都继承了events模块,比如http.fs等.本文将详细介绍nodeJS中的事件机制 EventEmitter 多数 Node.js ...
- Node.js入门:事件机制
Evented I/O for V8 JavaScript 基于V8引擎实现的事件驱动IO. 事件机制的实现 Node.js中大部分的模块,都继承自Event模块(http://n ...
- 【iScroll源码学习03】iScroll事件机制与滚动条的实现
前言 想不到又到周末了,周末的时间要抓紧学习才行,前几天我们学习了iScroll几点基础知识: 1. [iScroll源码学习02]分解iScroll三个核心事件点 2. [iScroll源码学习01 ...
- nodejs事件的监听与事件的触发
nodejs事件(Events) 一.事件机制的实现 Node.js中大部分的模块,都继承自Event模块(http://nodejs.org/docs/latest/api/events.html ...
- nodejs运行机制
有一天老大忽然问起我这个问题,nodejs运行机制 是怎样的?因自己对nodejs也不是很熟悉,就上网查了一下,得出结果如下: 1.简介 Node.js是一个事件驱动I/O服务端JavaScript环 ...
随机推荐
- UVa 1625 - Color Length(线性DP + 滚动数组)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 1、ClassLoader.getResourceAsStream() 与Class.getResourceAsStream()的区别
1.ClassLoader.getResourceAsStream() 与Class.getResourceAsStream()的区别 例如你写了一个MyTest类在包com.test.mycode ...
- java连接数据库增删改查公共方法
package dao; import java.io.IOException; import java.sql.CallableStatement; import java.sql.Connecti ...
- JAVA格式化解析日期
- Spark集群无法停止的原因分析和解决
今天想停止spark集群,发现执行stop-all.sh的时候spark的相关进程都无法停止.提示: no org.apache.spark.deploy.master.Master to stop ...
- JAVA给你讲它的故事
计算机语言如果你将它当做一个产品,就像我们平时用的电视机.剃须刀.电脑.手机等, 他的发展也是有规律的. 任何一个产品的发展规律都是:向着人更加容易使用.功能越来越强大的方向发展. 那么,我们的计算机 ...
- 初学pygame
#Author:cljimport pygamepygame.display.set_mode((640,480),0,32)#设置窗口大小 返回的也是一个surface对象,resolution可以 ...
- HDU 1411--校庆神秘建筑(欧拉四面体体积计算)
校庆神秘建筑 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- ubuntu8.04下mysql更改用户和密码
1.最近由于系统原因重装了mysql,但是发现安装过程中没有提示设置密码. 2.修改用户名和密码步骤 A.service mysql stop #停止mysql服务 B.sudo vim /et ...
- linux系统基础之---系统基本安全(基于centos7.4 1708)