nodejs(五)同步异步--USING SETTIMEOUT INSTEAD OF SETINTERVAL TO FORCE SERIALIZATION
Let’s say you want a function that does some I/O — such as parsing a log fi le — that will periodically
be executed. Let’s give that function the generic name my_async_function. You could start by
using setInterval like this:
var interval = 1000;
setInterval(function() {
my_async_function(function() {
console.log('my_async_function finished!');
});
},interval);
However, you must ensure that none of these functions execute at the same time. You really can’t
guarantee that if you are using setInterval. If my_async_function takes one millisecond longer
than the interval you are using, they would run at the same time.
You need to enforce that the interval between the end of one my_async_function and the start of
the next is the desired interval. Here is how you can do this:
var interval = 1000; // 1 second (function schedule() {
setTimeout(function do_it() {
my_async_function(function() {
console.log('async is done!');
schedule(); //再次调用
});
}, interval);
}());
Here you are declaring a function named schedule (line 3) and invoking it immediately after
it is declared (line 10). This schedule function schedules the do_it function to be executed in
one second (the chosen interval). After one second passes, this anonymous function fires, calling
my_async_function (line 5). When this function finishes, the anonymous callback you passed to it
is invoked (line 6), calling the schedule function, which will again schedule the do_it function to
be executed in one second, thus restarting the cycle.
nodejs(五)同步异步--USING SETTIMEOUT INSTEAD OF SETINTERVAL TO FORCE SERIALIZATION的更多相关文章
- nodejs(五)同步异步--BLOCKING THE EVENT LOOP
1.BLOCKING THE EVENT LOOP Node and JavaScript runtimes in general are single-threaded event loops. O ...
- {Python之进程} 背景知识 什么是进程 进程调度 并发与并行 同步\异步\阻塞\非阻塞 进程的创建与结束 multiprocess模块 进程池和mutiprocess.Poll
Python之进程 进程 本节目录 一 背景知识 二 什么是进程 三 进程调度 四 并发与并行 五 同步\异步\阻塞\非阻塞 六 进程的创建与结束 七 multiprocess模块 八 进程池和mut ...
- 前端笔记之JavaScript(九)定时器&JSON&同步异步/回调函数&函数节流&call/apply
一.快捷位置和尺寸属性 DOM已经提供给我们计算后的样式,但是还是觉得不方便,因为计算后的样式属性值都是字符串类型. 不能直接参与运算. 所以DOM又提供了一些API:得到的就是number类型的数据 ...
- NodeJS中的异步I/O、事件驱动
nodejs的主要特点是单线程.异步I/O.事件驱动.让我们先大概了解一下这些名词的意思. 单线程 单线程是任务按照顺序执行的,并且每次只执行一个任务,只有前面的任务执行完成以后,后面的任务才执行.在 ...
- 05慕课网《进击Node.js基础(一)》HTTP概念进阶(同步/异步)
HTTP模块介绍 支持http协议的更多特性 不缓存请求和响应 API比较底层处理流相关,信息解析 HTTP相关概念 回调 将函数作为参数传到执行函数中,参数函数在执行函数中嵌套执行 function ...
- Java IO 学习(一)同步/异步/阻塞/非阻塞
关于IO,同步/异步/阻塞/非阻塞,这几个关键词是经常听到的,譬如: “Java oio是阻塞的,nio是非阻塞的” “NodeJS的IO是异步的” 但是这些东西听多了就容易迷糊,比方说同步是否就是阻 ...
- nodejs中的异步回调机制
1.再次clear Timer定时器的作用 setTimeOut绝非是传统意义上的“sleep”功能,它做不到让主线程“熄火”指定时间,它是用来指定:某个回调在固定时间后插入执行栈!(实际执行时间略长 ...
- 深入理解nodejs中的异步编程
目录 简介 同步异步和阻塞非阻塞 javascript中的回调 回调函数的错误处理 回调地狱 ES6中的Promise 什么是Promise Promise的特点 Promise的优点 Promise ...
- 同步异步,阻塞非阻塞 和nginx的IO模型
同步与异步 同步和异步关注的是消息通信机制 (synchronous communication/ asynchronous communication).所谓同步,就是在发出一个*调用*时,在没有得 ...
随机推荐
- 一个简单的demo学习Android远程Service(AIDL的使用)
这是milo很早之前写在论坛上的一个帖子,现在整理出来,milo也复习一下一般来说Android 的四大组件都是运行在同一个进程中的,但远程Service运行在不同的进程里.这进程间的通信是使用了An ...
- Masonry — 使用纯代码进行iOS应用的autolayout自适应布局
本文转载至 http://www.ios122.com/2015/09/masonry/ 简化iOS应用使用纯代码机型自适应布局的工作,使用一种简洁高效的语法替代NSLayoutConstrain ...
- Android学习之PopupWindow
Android的对话框有两种:PopupWindow和AlertDialog. 详细说明如下: AlertDialog是非阻塞式对话框:AlertDialog弹出时,后台还可以做事情: AlertDi ...
- 使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
//使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前 //2008年03月15日 星期六 02:35 public string DateStringFromNow(DateTim ...
- Linq-string判断忽略大小写
Coupon203Play play = dbContext.Coupon203Plays.Where(u => u.VerifyCode.Equals(verifyCode,StringCom ...
- open-falcon之judge
功能 judge 模块主要从transfer中接收数据,并从HBS中获取报警策略,然后进行阈值报警判断 从HBS获取报警策略 接收transfer 上报的数据,并存储最新几个点 判断阈值,产生报警事件 ...
- IOS多线程之序
版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们开发的应用基本上都是多线程的,几乎没有不是多线程的应用发布在appstore.首先我们的应用启动会默认有一个主线程,你一直在里面执行很多操作 ...
- sencha touch 可自动增长高度TextArea
js代码如下: /* *高度自动增长的文本框 */ Ext.define('ux.TextArea', { extend: 'Ext.field.TextArea', xtype: 'autoText ...
- 提交代码到自己的github仓库
说起来尴尬,好几年前就搞了github建了仓库,当时玩得还有点6,后来一直的公司都是svn,自己业务项目也没玩,都忘了要怎么提交代码到自己的仓库了. 这边再来一波记录吧. 一.配置用户名 git co ...
- nagios监控报错 It appears as though you do not have permission to view...
今天在安装完nagios后,通过nagios网页界面点击主机.服务.问题页面时.均报错,报错的内容都差不多.如点击服务,报错: It appears as though you do not have ...