nodejs之2
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了nodejs之2,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3320字,纯文字阅读大概需要5分钟。
内容图文
实现定时器
- setTimeout(callback,delayMilliSeconds,[args]);取消超时时间函数:clearTimeout(timeoutId);
mytimeout=setTimeout(myfunc,1000);
clearTimeout(mytimeout);
- setInterval(callback,delayMilliSeconds,[args]);取消间隔函数:clearInterval(timeoutId);
myInterval=setTimeout(myfunc,1000);
clearInterval(myInterval);
- setImmediate(callback,[args]);取消即时函数clearImmediate(immediateId);
myImmediate=setTimeout(myfunc,1000);
clearImmediate(myImmediate);
从事件循环中取消定时器引用
myInterval=setTimeout(myfunc,1000);
myInterval.unref();
恢复引用
myInterval.ref();
使用nextTick来调度工作
process.nextTick(callback)
Nodejs使用默认值为1000的process.maxTickDepth来限制事件队列的每次循环可执行的nextTick()时间数目。
实现事件发射器和监听器
事件使用一个EventEmitter对象来发出。这个对象在events模块中。emit(eventName,[args])函数来触发eventName事件,包括提供的任何参数
1 var event=require(‘event‘); 2var emitter=new event.EventEmitter(); 3 emitter.emit(‘simpleEvent‘);
把事件直接添加到自己的js对象。如下面:
function MyObj(){ Event.EventEmitter.call( this ); } MyObj.prototype.__proto__=events.EventEmitter.prototype;
然后你就可以直接从对象实例中发出事件。例如:
var myobj=new MyObj(); myobj.emit("someEvent");
把事件监听器添加到对象
- .addListener(eventName,callback):每当eventName事件被触发时,回调函数就被放置在事件队列中执行。
- .on(eventName,callback):同上。
- .once(eventName,callback):只有eventName事件第一次被触发时回调函数才被放置在事件队列中执行。
从对象中删除监听器
- .listeners(eventName):返回一个连接到eventName事件的监听器函数的数组。
- .setMaxListeners(n):如果多于n的监听器都加入到EventEmitter对象就触发警报。他的默认值为10.
- .removeListener(eventName,callback):将callback函数从Eventemitter对象的eventName事件中删除。
实现事件监听器和发射器事件
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 checkOverdraw(){ if(this.balance<0){ console.log("哎妈呀,超支了!"); } } function displayBalance(){ console.log("Account balance: $%d",this.balance); } function checkGoal(acc,goal){ if(acc.balance>goal){ console.log("你是个有钱人!"); } } 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);
实现回调
回调的三个具体实现:将参数传递给回调函数,在循环内处理回调函数参数,以及嵌套回调。
向回调函数传递额外的参数的方法:在一个匿名函数中实现该参数,然后用来自匿名函数的参数调用回调函数。如下:
var events=require("events"); function CarShow(){ events.EventEmitter.call(this); this.seeCar=function(make){ this.emit("sawCar",make); }; } CarShow.prototype.__proto__=events.EventEmitter.prototype; var show=new CarShow(); function logCar(make){ console.log("Saw a "+make); } function logColorCar(make,color){ console.log("Saw a %s %s",color,make); } show.on("sawCar",logCar); show.on("sawCar",function(make){ var colors=[‘red‘,‘blue‘,‘black‘]; var color=colors[Math.floor(Math.random()*3)]; logColorCar(make,color); }); show.seeCar("喵喵"); show.seeCar("肥秒"); show.seeCar("可爱喵"); show.seeCar("美丽妙");
链式回调
function logCar(car,callback){ console.log("Saw a %s ",car); if(cars.length){ process.nextTick(function(){ callback(); }); } } function logCars(cars){ var car=cars.pop(); logCar(car,function(){ logCars(cars); }); } var cars=["喵喵","妙妙","肥秒","纯天然妙","傻逼喵","我爱的妙"]; logCars(cars);
PS:如有问题,请大神之处
原文:http://www.cnblogs.com/dongzixiansheng/p/7020479.html
内容总结
以上是互联网集市为您收集整理的nodejs之2全部内容,希望文章能够帮你解决nodejs之2所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。