标签:nts 移除 alt 继承 change 监听器 icon 支持 request
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);
标签:nts 移除 alt 继承 change 监听器 icon 支持 request
原文地址:http://www.cnblogs.com/lyy-2016/p/6684055.html