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的更多相关文章

  1. nodejs(五)同步异步--BLOCKING THE EVENT LOOP

    1.BLOCKING THE EVENT LOOP Node and JavaScript runtimes in general are single-threaded event loops. O ...

  2. {Python之进程} 背景知识 什么是进程 进程调度 并发与并行 同步\异步\阻塞\非阻塞 进程的创建与结束 multiprocess模块 进程池和mutiprocess.Poll

    Python之进程 进程 本节目录 一 背景知识 二 什么是进程 三 进程调度 四 并发与并行 五 同步\异步\阻塞\非阻塞 六 进程的创建与结束 七 multiprocess模块 八 进程池和mut ...

  3. 前端笔记之JavaScript(九)定时器&JSON&同步异步/回调函数&函数节流&call/apply

    一.快捷位置和尺寸属性 DOM已经提供给我们计算后的样式,但是还是觉得不方便,因为计算后的样式属性值都是字符串类型. 不能直接参与运算. 所以DOM又提供了一些API:得到的就是number类型的数据 ...

  4. NodeJS中的异步I/O、事件驱动

    nodejs的主要特点是单线程.异步I/O.事件驱动.让我们先大概了解一下这些名词的意思. 单线程 单线程是任务按照顺序执行的,并且每次只执行一个任务,只有前面的任务执行完成以后,后面的任务才执行.在 ...

  5. 05慕课网《进击Node.js基础(一)》HTTP概念进阶(同步/异步)

    HTTP模块介绍 支持http协议的更多特性 不缓存请求和响应 API比较底层处理流相关,信息解析 HTTP相关概念 回调 将函数作为参数传到执行函数中,参数函数在执行函数中嵌套执行 function ...

  6. Java IO 学习(一)同步/异步/阻塞/非阻塞

    关于IO,同步/异步/阻塞/非阻塞,这几个关键词是经常听到的,譬如: “Java oio是阻塞的,nio是非阻塞的” “NodeJS的IO是异步的” 但是这些东西听多了就容易迷糊,比方说同步是否就是阻 ...

  7. nodejs中的异步回调机制

    1.再次clear Timer定时器的作用 setTimeOut绝非是传统意义上的“sleep”功能,它做不到让主线程“熄火”指定时间,它是用来指定:某个回调在固定时间后插入执行栈!(实际执行时间略长 ...

  8. 深入理解nodejs中的异步编程

    目录 简介 同步异步和阻塞非阻塞 javascript中的回调 回调函数的错误处理 回调地狱 ES6中的Promise 什么是Promise Promise的特点 Promise的优点 Promise ...

  9. 同步异步,阻塞非阻塞 和nginx的IO模型

    同步与异步 同步和异步关注的是消息通信机制 (synchronous communication/ asynchronous communication).所谓同步,就是在发出一个*调用*时,在没有得 ...

随机推荐

  1. 深度缓存ZBuffer线性化

    double linearizeDepth(double nearz,double farz,double depth) { depth = 2.0 * depth - 1.0; return (2. ...

  2. 编译poco-1.7.8

    运行build_vs140.cmd,运行之前可以修改一些参数,例如编译64位 buildwin 140 build shared both x64 nosamples notests devenv 修 ...

  3. Mac终端解压命令集合

    tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压1 ...

  4. java虚拟机性能监控调优及原则

    摘抄 http://uule.iteye.com/blog/2114697 一.JVM内存模型及垃圾收集算法  1.根据Java虚拟机规范,JVM将内存划分为: New(年轻代) Tenured(年老 ...

  5. Java中过滤器和拦截器的区别

    1.拦截器是基于java反射机制的,而过滤器是基于函数回调的. 2.过滤器依赖于servlet容器,而拦截器不依赖于servlet容器. 3.拦截器只对action起作用,而过滤器几乎可以对所有请求起 ...

  6. 《Windows内核编程》---系统线程和同步事件

    系统线程: 在驱动中生成的线程一般是系统线程,系统线程所在的进程名为“System”,用到的内核API函数是: NTSTATUS PsCreateSystemThread( OUT PHANDLE T ...

  7. JS函数匿名替换

    //匿名替换函数 function objFunc() { var obj = new Object(); obj.JsonData = [{ aa: "}], obj.FilterData ...

  8. 浏览器 User Agent字符串列表

    http://www.73207.com/useragent/ http://www.73207.com/useragent/pages/internet-2520explorer/index.htm ...

  9. Elasticsearch学习之head插件安装

    通过elasticseach自带的plugin命令 elasticsearch/bin/plugin -install mobz/elasticsearch-head 如下图:  2. zip包安装 ...

  10. create-react-app项目中的eslint

    "no-multi-spaces": 1, //禁止多个空格 "jsx-quotes": 1, //此规则强制在JSX属性中一致使用双引号或单引号 " ...