简体   繁体   中英

Understanding Threading in javascript

I have two div's in my html page

<div id="box1">
</div>

<div id="box2">
</div>

Both of them are absolutely positioned at different positions.

#box1,#box2 {
    height: 100px;
    width: 100px;
    background-color: red;
    position: absolute;
}

#box2 {
    top :200px;
}

In my Javascript I am animating these two div's as shown below

$(document).ready(function() {

    function animateBoxes() {
        $("#box1").animate({left : "500px"},5000);
        $("#box2").animate({left : "500px"},3000);
    }

    animateBoxes();
});

When my page loads both the div's start moving at the same time.

My question is if javascript is single threaded how can both the div's move at the same time. Because the movement of the div's is handled by javascript by two animate functions, one on box1 and other on box2.

How both the animate functions got executed at the same time?

Javascript is single threaded meaning something if is stuck the whole script is stuck...and only way overcome this is spawn worker . To answer your question, Do You Know how animate function of jquery works...it sets timer to call function that updates div's position. So both div get positioned a little bit toward their goal. Timer is provided by javascript and is handled like an event. Javascript has event loop..which is reiterated by browser. Meaning as soon as js is done browser goes through all events and check if they have been fired and then run the function that is associated with them. Here animate function associate itself to timer event and gradually updates div's position, thus looking like it animated. And since it happens in steps the whole js doesn't have to wait for this animation to end to continue executing.

This is how events basically work in js:

  • browser starts executing the code..
  • every next action waits till last action is done.
  • when javascript reaches the code that is attaching function to event, js registers the event, let's say click event. Now it know that button has click event and it has this function.
  • Now after all code below has been executed browser starts new routine. To check for any event that have been fired.
  • ...looping but no events..
  • you click button ...it adds that click event has fired to event loop check list.
  • browser again checks the event loop...it sees the event.
  • it runs the code for that event and clear it...
  • suppose you clicked two times...then the code of second event won't start executing till the first is done.
  • Timer is same but it fires every x amount of time has passed. But as you can see event loops gets stucked executing the function related to the event...the timer is not perfect.
  • let's say you set timer for 10 ms. And as 9ms had passed you clicked the button starting another event. So your button event starts executing..but it did something really long that took 5 ms.
  • so your timer actually fires at 14ms.

Take a look at this example: http://jsfiddle.net/82zLC/6/

This will give you idea that animation is divided into chunks which are update step by step...try changing t to 60.

JavaScript can act asynchronous in many situations. .animate() is an example of this. It pretty much has to act asynchronous, in order to not interrupt other page processes. If you are looking for the events to happen one-after-the-other, try looking into callbacks:

$("#box1").animate({left: "500px"},5000, function(){
    $("#box2").animate({left: "500px"},5000);
});

When we pass the function to .animate() , it calls the function after it is done with the animation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM