0

let me start with an example..

I am working on my web (Java work) and I am using AJAX to dump a big data in db. I am running an separate thread for it. That works exactly in a way I expect.

But I'm confused about something.. so , let's say (for demo) I've the following code

function fun1() {
  console.log("x");
  alert("x");
  return 0;
}

function fun2() {
  var delay = 3000; //3 second
  setTimeout(function() {
    console.log("y");
    alert("y");
  }, delay);
}
<input type="submit" class="funfoo" onclick="fun1()" />
<input type="submit" class="funfoo" onclick="fun2()" />

And I know that Jquery is single threaded. And I am with C++ background so I am simply comparing single threaded concepts of C++ with jQuery (that's obvious and that is how it should work).

But I just don't grab how it still executes both the functions written above in the script ?

When I click on second button and instantly on the first button after that I still see y in my console window after 3 seconds after x is written in console. That gives me a feeling like there should be thread running in background and all that.

So I was thinking why do I then need to run a separate thread to dump my data when (I don't know how but I does) it'll be done automatically by some logic that I am unfamiliar with for the moment.

Hope my problem is understood. (Sorry for improper title of the question)

3
  • 2
    1. There's no JQuery here that I can see. 2. It probably queues both into an event queue, and executes them sequentially. timeout probably parks the callback, executes the other function, then starts the task again. That's how I've always assumed it worked. Commented Jan 21, 2017 at 19:53
  • Here's the gist of it developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop Commented Jan 21, 2017 at 19:56
  • 1
    thanks for the source link @nem035 . I am gonna go through it for the concepts :) Commented Jan 21, 2017 at 19:57

1 Answer 1

1

When I click on second button and instantly on the first button after that I still see y in my console window after 3 seconds after x is written in console. That gives me a feeling like there should be thread running in background and all that.

You set a timeout for an action to be executed 3 seconds from now, and after 3 seconds, the action runs. That does not mean that there has been a thread running through those 3 seconds.

As you know there's only one thread. In your example, this is what the thread is doing:

  1. (do nothing, since there's nothing to do)

  2. an event fired! (a button was pressed). This makes the thread run function 2, setting a timeout.

  3. (finished the execution of function 2, do nothing)

  4. an event fired! (a button was pressed). This makes the thread run function 1.

  5. (finished the execution of function 1, do nothing)

  6. an event fired! (a timeout finished). This makes the thread run the associated function

Note that if the single thread is busy when the timeout finishes (or a button is pressed, or any other event) the function that is binded to the event will not be run immediately. Instead, it will be placed in a queue to be run when the thread becomes available.

(note that I'm not an expert in the internals of JavaScript, so some of these facts could be wrong if taken literally. That's just a mental model that has worked well for for me)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.