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).所谓同步,就是在发出一个*调用*时,在没有得 ...
随机推荐
- jinja语法
<!--base.html--> <!DOCTYPE html> <html lang="en"> <head> <!--ht ...
- flask + mysql写的简单监控系统
这里以监控内存使用率为例,写的一个简单demo性程序,具体操作根据51reboot提供的教程写如下. 一.建库建表 创建falcon数据库: mysql> create database fal ...
- C语言中如何计算时间差
#include <time.h> #include <stdio.h> int main() { time_t start ,end ; ...
- ip防刷脚本
#!/bin/sh #防刷脚本 #env ACCESS_PATH=/home/wwwlogs ACCESS_LOG=y.log IPTABLES_TOP_LOG=iptables_top.log DR ...
- 汉字按首字母排序(javascript,php,mysql实现)
1.javascript实现 var a = ["啊","得啊_123","得啊_0124","波啊","婆& ...
- hive报错: Specified key was too long; max key length is 767 bytes
废话不多说,报错如下: DataNucleus.Datastore (Log4JLogger.java:error(115)) - An exception was thrown while addi ...
- IDA + VMware 调试win7 x64
IDA+gdb配合VMware调试windows已经不是什么新鲜事了,但是之所以要发这篇帖子是因为我按照之前的帖子还有网上其他的教程设置调试环境,结果遇到了各种问题,所以仅仅是更新一下,各位轻拍. 环 ...
- HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP错误解决方法
在树莓派上运行在windows上正确的程序, 报错: HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP OpenCV Error: Assertion failed (s ...
- Material Design系列第一篇——Creating Apps with Material Design
Creating Apps with Material Design //创建Material Design的App Material design is a comprehensive guide ...
- 剑指offer——35复杂链表的复制
这题很是巧妙. 突破了常规思维. 竟然可以把传入进来的链表和复制的链表链在一起.然后再算出slibling指针.最后在分离. 直接把空间复杂度变为O(1)了. 很巧妙,很实用. 题目: 请实现函数Co ...