<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    gembin

    OSGi, Eclipse Equinox, ECF, Virgo, Gemini, Apache Felix, Karaf, Aires, Camel, Eclipse RCP

    HBase, Hadoop, ZooKeeper, Cassandra

    Flex4, AS3, Swiz framework, GraniteDS, BlazeDS etc.

    There is nothing that software can't fix. Unfortunately, there is also nothing that software can't completely fuck up. That gap is called talent.

    About Me

     

    How JavaScript Timers Work

    At a fundamental level it’s important to understand how JavaScript timers work. Often times they behave unintuitively because of the single thread which they are in. Let’s start by examining the three functions to which we have access that can construct and manipulate timers.

    • var id = setTimeout(fn, delay); – Initiates a single timer which will call the specified function after the delay. The function returns a unique ID with which the timer can be canceled at a later time.
    • var id = setInterval(fn, delay); – Similar to setTimeout but continually calls the function (with a delay every time) until it is canceled.
    • clearInterval(id);clearTimeout(id); – Accepts a timer ID (returned by either of the aforementioned functions) and stops the timer callback from occurring.

    In order to understand how the timers work internally there’s one important concept that needs to be explored: timer delay is not guaranteed. Since all JavaScript in a browser executes on a single thread asynchronous events (such as mouse clicks and timers) are only run when there’s been an opening in the execution. This is best demonstrated with a diagram, like in the following:


    (Click to view full size diagram)

    There’s a lot of information in this figure to digest but understanding it completely will give you a better realization of how asynchronous JavaScript execution works. This diagram is one dimensional: vertically we have the (wall clock) time, in milliseconds. The blue boxes represent portions of JavaScript being executed. For example the first block of JavaScript executes for approximately 18ms, the mouse click block for approximately 11ms, and so on.

    Since JavaScript can only ever execute one piece of code at a time (due to its single-threaded nature) each of these blocks of code are “blocking” the progress of other asynchronous events. This means that when an asynchronous event occurs (like a mouse click, a timer firing, or an XMLHttpRequest completing) it gets queued up to be executed later (how this queueing actually occurs surely varies from browser-to-browser, so consider this to be a simplification).

    To start with, within the first block of JavaScript, two timers are initiated: a 10mssetTimeout and a 10ms setInterval. Due to where and when the timer was started it actually fires before we actually complete the first block of code. Note, however, that it does not execute immediately (it is incapable of doing that, because of the threading). Instead that delayed function is queued in order to be executed at the next available moment.

    Additionally, within this first JavaScript block we see a mouse click occur. The JavaScript callbacks associated with this asynchronous event (we never know when a user may perform an action, thus it’s consider to be asynchronous) are unable to be executed immediately thus, like the initial timer, it is queued to be executed later.

    After the initial block of JavaScript finishes executing the browser immediately asks the question: What is waiting to be executed? In this case both a mouse click handler and a timer callback are waiting. The browser then picks one (the mouse click callback) and executes it immediately. The timer will wait until the next possible time, in order to execute.

    Note that while mouse click handler is executing the first interval callback executes. As with the timer its handler is queued for later execution. However, note that when the interval is fired again (when the timer handler is executing) this time that handler execution is dropped. If you were to queue up all interval callbacks when a large block of code is executing the result would be a bunch of intervals executing with no delay between them, upon completion. Instead browsers tend to simply wait until no more interval handlers are queued (for the interval in question) before queuing more.

    We can, in fact, see that this is the case when a third interval callback fires while the interval, itself, is executing. This shows us an important fact: Intervals don’t care about what is currently executing, they will queue indiscriminately, even if it means that the time between callbacks will be sacrificed.

    Finally, after the second interval callback is finished executing, we can see that there’s nothing left for the JavaScript engine to execute. This means that the browser now waits for a new asynchronous event to occur. We get this at the 50ms mark when the interval fires again. This time, however, there is nothing blocking its execution, so it fires immediately.

    Let’s take a look at an example to better illustrate the differences betweensetTimeout and setInterval.

    1. setTimeout(function(){
    2.     /* Some long block of code... */
    3.     setTimeout(arguments.callee, 10);
    4.   }, 10);
    5.  
    6.   setInterval(function(){
    7.     /* Some long block of code... */
    8.   }, 10);

    These two pieces of code may appear to be functionally equivalent, at first glance, but they are not. Notably the setTimeout code will always have at least a 10ms delay after the previous callback execution (it may end up being more, but never less) whereas the setInterval will attempt to execute a callback every 10ms regardless of when the last callback was executed.

    There’s a lot that we’ve learned here, let’s recap:

    • JavaScript engines only have a single thread, forcing asynchronous events to queue waiting for execution.
    • setTimeout and setInterval are fundamentally different in how they execute asynchronous code.
    • If a timer is blocked from immediately executing it will be delayed until the next possible point of execution (which will be longer than the desired delay).
    • Intervals may execute back-to-back with no delay if they take long enough to execute (longer than the specified delay).

    All of this is incredibly important knowledge to build off of. Knowing how a JavaScript engine works, especially with the large number of asynchronous events that typically occur, makes for a great foundation when building an advanced piece of application code.

    from: http://ejohn.org/blog/how-javascript-timers-work/


    posted on 2013-05-27 13:50 gembin 閱讀(604) 評論(0)  編輯  收藏 所屬分類: Javascript

    導航

    統計

    常用鏈接

    留言簿(6)

    隨筆分類(440)

    隨筆檔案(378)

    文章檔案(6)

    新聞檔案(1)

    相冊

    收藏夾(9)

    Adobe

    Android

    AS3

    Blog-Links

    Build

    Design Pattern

    Eclipse

    Favorite Links

    Flickr

    Game Dev

    HBase

    Identity Management

    IT resources

    JEE

    Language

    OpenID

    OSGi

    SOA

    Version Control

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    free counters
    主站蜘蛛池模板: 无码毛片一区二区三区视频免费播放 | 国产亚洲一卡2卡3卡4卡新区| 曰曰鲁夜夜免费播放视频| 亚洲成A∨人片在线观看不卡| 国产又黄又爽胸又大免费视频| 亚洲国产成人精品91久久久| 国产精品亚洲专区无码唯爱网| 日韩免费福利视频| 亚洲国产精品99久久久久久| 毛片基地免费观看| 亚洲日韩国产二区无码| 免费看美女被靠到爽的视频| 亚洲国产精品久久久久秋霞小| 国产免费无遮挡精品视频| 狠狠综合亚洲综合亚洲色| 亚洲AV永久无码精品一区二区国产| 香港特级三A毛片免费观看| 亚洲国产精品自产在线播放| 99视频在线观看免费| 亚洲欧洲日产国码久在线观看| 免费A级毛片无码A∨中文字幕下载| 久久久久久久亚洲Av无码| 成人免费一级毛片在线播放视频| 亚洲伦理中文字幕| 日本人的色道www免费一区| 免费大片av手机看片高清| 亚洲日韩欧洲无码av夜夜摸| 日韩免费无码视频一区二区三区| 亚洲一区中文字幕| 日韩亚洲国产综合久久久| 色播在线永久免费视频网站| 亚洲美女色在线欧洲美女| 在线免费观看一级毛片| aaa毛片视频免费观看| 亚洲国产成人精品青青草原| 免费一级毛片不卡不收费| 久久精品免费观看| 亚洲中文字幕无码爆乳| 亚洲色欲久久久综合网东京热| 免费看男女下面日出水来| 免费人成网站永久|