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)
timeoutprobably parks the callback, executes the other function, then starts the task again. That's how I've always assumed it worked.