Javascript with Chorme v8 engine works like this :

For Chorme engine, v8, it has call stack.

And all the async opreations functions are stay in webapis. So everytime  you call 'setTimeout()' or http call, it will always call webapis.

So, in the picture,

we call the main() function, and push into the call stack;

then console.log("Hi"); put into call stack and finish then poped up from the call stack;

third, setTimeout() function run into the call stack, then it call webapis with callback function, so push into webapis. And setTimeout() popup from the call stack.

fourth, console.log('JSC'); push into the call stack and poped up.

Finally main() finished and poped up:

So what left here is just webapis have a callback function, waiting 5 second, when the time out, it was push to the task queue, NOT to the call stack.

Now the 'Event loop' come in, what event loop does is: it check whether there is any task in the call stack.... if yes, finish those functions in the calll stack first. If not, then event loop checks the task queue then push the callback from the task queue to call stack.

Then console.log() function run and poped up from the call stack.

-----------------------------------------

You might have met the problem that you run setTimeout(fn(), 0); it wait no time, but the fn() come at last:

function one(){
console.log("One");
} function two(){
console.log("Two");
} function delay(){
console.log("Delay");
} one();
setTimeout(delay, 0);
two(); /*
"One"
"Two"
"Delay"
*/

That is because, setTimeout is webapis, so the callback function delay() will be pushed into the 'task queue'. It will wait all the functions in 'call stack' finish, then even loop will check the task queue and push the callback to the call stack. So the "Delay" console log out at the bottom.

Link: https://www.youtube.com/watch?v=8aGhZQkoFbQ

[Javascript] Task queue & Event loop.的更多相关文章

  1. JavaScript事件循环(Event Loop)机制

    JavaScript 是单线程单并发语言 什么是单线程 主程序只有一个线程,即同一时间片断内其只能执行单个任务. 为什么选择单线程? JavaScript的主要用途是与用户互动,以及操作DOM.这决定 ...

  2. 详解JavaScript中的Event Loop(事件循环)机制

    前言 我们都知道,javascript从诞生之日起就是一门单线程的非阻塞的脚本语言.这是由其最初的用途来决定的:与浏览器交互. 单线程意味着,javascript代码在执行的任何时候,都只有一个主线程 ...

  3. JavaScript 事件循环 — event loop

    引言 相信所有学过 JavaScript 都知道它是一门单线程的语言,这也就意味着 JS 无法进行多线程编程,但是 JS 当中却有着无处不在的异步概念 .在初期许多人会把异步理解成类似多线程的编程模式 ...

  4. 一文梳理JavaScript 事件循环(Event Loop)

    事件循环(Event Loop),是每个JS开发者都会接触到的概念,但是刚接触时可能会存在各种疑惑. 众所周知,JS是单线程的,即同一时间只能运行一个任务.一般情况下这不会引发问题,但是如果我们有一个 ...

  5. 从Javascript单线程谈Event Loop

    假如面试回答js的运行机制时,你可能说出这么一段话:"Javascript的事件分同步任务和异步任务,遇到同步任务就放在执行栈中执行,而碰到异步任务就放到任务队列之中,等到执行栈执行完毕之后 ...

  6. 深入理解Javascript单线程谈Event Loop

    假如面试回答js的运行机制时,你可能说出这么一段话:"Javascript的事件分同步任务和异步任务,遇到同步任务就放在执行栈中执行,而碰到异步任务就放到任务队列之中,等到执行栈执行完毕之后 ...

  7. JavaScript 运行机制 (Event Loop)

    单线程就意味着,所有任务需要排队,前一个任务结束,才会执行后一个任务.如果前一个任务耗时很长,后一个任务就不得不一直等着. 所有任务可以分成两种,一种是同步任务(synchronous),另一种是异步 ...

  8. Javascript之Event Loop

    先看段代码: console.log(1); setTimeout(function () { console.log(2); new Promise(function (resolve, rejec ...

  9. JavaScript 运行机制详解:Event Loop

    参考地址:http://www.ruanyifeng.com/blog/2014/10/event-loop.html 一.为什么JavaScript是单线程? JavaScript语言的一大特点就是 ...

随机推荐

  1. hdu5067Harry And Dig Machine(TSP旅行商问题)

    题目链接: huangjing 题意:给出一幅图.图中有一些点,然后从第1个点出发,然后途径全部有石头的点.最后回到原点,然后求最小距离.当初作比赛的时候不知道这就是旅行商经典问题.回来学了一下. 思 ...

  2. 收集的URL

    *******************************************看文章的好地方************************************** http://www. ...

  3. void*指针

    C++提供了一种特殊的指针类型void*,它可以保存任何类型对象的地址: void*表明该指针与一地址值相关,但不清楚存储在此地址上的对象的类型. void*指针只支持几种有限的操作: 1)与另一个指 ...

  4. Java基础知识强化36:StringBuffer类之StringBuffer的概述

    1. StringBuffer类概述: (1)String的缺陷: 我们如果对字符串进行拼接操作,每次拼接,都会构造一个新的String对象,既耗时,又浪费空间.如下图: (2)StringBuffe ...

  5. .IIS7如何设置301重定向详解

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...

  6. $http post传值的问题

    var app = angular.module("myApp", [], function ($httpProvider) { $httpProvider.defaults.he ...

  7. eclipse python开发环境搭建

    eclipse python开发环境搭建[非原创] 1.在www.eclipse.org官网下载Eclipse Classic 4.2.2,Win7 64位下载eclipse-SDK-4.2.2-wi ...

  8. (原)python使用ctypes调用C/C++接口

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6135514.html 参考网址: https://docs.python.org/2/library/ ...

  9. Javascript 判断浏览器是否为IE的最短方法

    作者:idd.chiang 发布时间:April 29, 2010 分类:Javascript/AS 在网上有幸看到夷人通过IE与非IE浏览器对垂直制表符支持特性搞出的一段简短的条件: var ie ...

  10. 标准库biset

    bitset<n> b; b 有 n 位,每位都 . bitset<n> b(u); b 是 unsigned long 型 u 的一个副本. bitset<n> ...