…if the last command doesnt have a callback function? Like for example
$(something).remove(other_thing).append(new_thing)
Append doesnt have a callback function, so my question is, is there a jQuery method (or other way) to call a passed function after a chain of commands? The function doesnt have to affect the current selection.
EDIT: I know the methods in this example shouldnt take long so a callback wouldnt be necessary, but that was just the first example I could think of. so please think of it as if this wasnt the case
To expand on Craig’s answer, you simply invoke your function normally in the next statement.
The purpose of a callback function is so that you can do something after something else—which will take an indeterminate amount of time, like a network (AJAX) call—completes. Key to this is that the other operation does not block. In other words, it happens—or, from your script’s perspective, appears to happen—on another thread.
Consider how an AJAX call works.
The call to
$.getexecutes first in this program. The first parameter is the URL you want to fetch; the second is an anonymous callback. It will be executed after the browser connects to the server, requests/something, and eventually receives a response. These things happen in the background while your script continues to execute.The call to
console.logexecutes after the call to$.getreturns. Since$.getreturns immediately, the log call happens before the browser has even had a chance to connect to the server. This meansdwill be undefined, and your call toconsole.logwill just show “undefined”.This is where the usefulness of a callback comes in to play. When the background request completes, the browser fires an event on the XHR object that jQuery used to issue your request. jQuery then calls your callback function with the result of the request. In this function, we now have the data, can set
d, and the call toalerthas your data. Magic!Now consider how jQuery’s DOM manipulation functions (like
append) work. They are synchronous. That means that your call toappendblocks—and your script can do nothing else—until the function has completed all of its work.This is a consequence of how the browser’s native DOM manipulation functions work. They don’t return until the change to the DOM is complete.
Thus, it is not useful for jQuery to provide callback functions on DOM manipulation methods (or any other synchronous method).
Here’s what
appendlooks like from a vastly simplified standpoint:Here’s what it would look like if we added the option to have a callback:
And now, what you would do from your code, respectively:
These two snippets are guaranteed to execute exactly the same, every time.
.append. jQuery works some if its magic, and eventually calls the nativeappendChildon#something.appendChildappends the new node(s) to#something. The DOM is modified, and internal data structures are updated to reflect the changes. If you are appending many nodes, this might take a while.appendChild..append().addBar().addBaris done, the next statement,console.log('Done')is called..append()checks if you’ve supplied a callback, and you have, so….append()calls the callback function.addBar().addBaris done, control returns to the callback function, and since there’s nothing else in the function, it returns as well..append(), and there is nothing left to execute in that function, so control returns from.append().console.log('Done')is called.As you can see, no matter which way we do it,
addBar()is called.console.log()is called.As a rule: If a jQuery function does not provide the option to pass a callback, the function is synchronous, and therefore you can just proceed with your next step on the next line.
Edit: If you want to call a function in the middle of a chain, jQuery doesn’t provide anything built-in, but we can write our own plugin!
This takes the function you want to call as the first argument, then passes the rest of the arguments to that function. Within the function,
thisrefers to the jQuery object from your chain. The function’s return value is ignored. Demo.