How does jQuery have asynchronous functions?

Posted by Sam.Rueby on Stack Overflow See other posts from Stack Overflow or by Sam.Rueby
Published on 2012-03-22T23:12:50Z Indexed on 2012/03/22 23:29 UTC
Read the original article Hit count: 208

I'm surprised I can't find a clear answer to this. So, in jQuery, you can do this:

$(someElements).fadeOut(1000);
$(someElements).remove();

Which, will start a fadeOut animation, but before it finishes executing in the 1 second duration, the elements are removed from the DOM. But how is this possible? I keep reading the JavaScript is single threaded. ( Is javascript guaranteed to be single-threaded? ) This question is not "How do I fix this?" I know I can do either: $(someElements).fadeOut(1000).promise().done(function() { $(someElements).remove();});, or even better:$(someElements).fadeOut(1000, function() { $(this).remove(); } );

What I don't understand is how JavaScript runs in a "single thread" but I'm able to use these jQuery functions that execute asynchronously and visibly see the DOM change in different places at the same time. How does it work?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery