Below is the code snippet of JQuery code for Chaining vs Callback Function:-
$(selector).animate({ opacity: 1 }).animate({ opacity: 0.5 });
vs
$(selector).animate({ opacity: 1}, function()
{
$(this).animate({ opacity: 0.5 });
});
In what type(s) of situation would one want to prefer one over the other?
They are effectively the same, so you’d probably just use the first.
Callbacks (the second version) are for running any arbitrary code that isn’t automatically queued.
This includes other jQuery methods like
.css()for example, which if not in the callback, will run long before the animation is complete.Without the callback…