jQuery Callback


Line by line, JavaScript statements are executed. However, because jQuery effects take some time to complete, the code on the next line may execute while the previous effect is still running. To avoid this, jQuery provides a callback function for each effect method.

A callback function is a function that is called after the effect is over. The callback function is provided as an argument to the effect methods, and it is usually the last parameter. For example, the basic syntax of the jQuery slideToggle() effect method with a callback function is: 

Syntax

Consider the following example, in which the slideToggle() and alert() lines are positioned next to each other. If you use this code, the alert will be displayed instantly once you hit the trigger button, without having to wait for the slide toggle effect to finish.

Example

Preview

And here's a modified version of the previous example in which the alert() statement is included within a callback function for the slideToggle() method. If you use this code, an alert message will appear after the slide toggling effect has finished.

Example

Preview

Similarly, you may construct callback functions for jQuery effect methods such as show(), hide(), fadeIn(), fadeOut(), animation(), and so on.

Example

Preview

If you run the above code, it will display the identical alert message twice, one for each <h1> and <p> element, when you click the trigger button.

FAQs

In jQuery, a callback function is a function that is passed as an argument to another function and is executed once the first function completes its task. Callback functions are commonly used in asynchronous operations, such as animations or AJAX requests, where you need to perform additional actions after a certain task is finished. For example, in an animation, you can define a callback function that gets executed when the animation is complete. This allows you to chain multiple animations or perform other tasks after the animation finishes. By utilizing callback functions, you can control the flow of your code and ensure that specific actions are executed at the right time, enhancing the interactivity and responsiveness of your web page.

jQuery's animation methods, such as animate() or slideToggle(), provide the option to include a callback function as a parameter. This callback function will be executed once the animation is complete. For example, $('.my-element').animate({ opacity: 0.5 }, 1000, function() { console.log('Animation complete!'); }) sets a callback function that logs a message to the console when the animation finishes. You can use this callback function to perform additional actions, chain animations, or trigger other events after the animation completes. By utilizing the callback feature, you can create dynamic and interactive animations that respond to user interactions or update other elements on your web page based on the completion of the animation.

Yes, you can pass arguments to callback functions in jQuery. When defining your callback function, you can include parameters to receive the values passed from the function that invokes the callback. For example, $('.my-element').fadeIn(1000, function(speed) { console.log('Fade in complete at speed: ' + speed); }) sets a callback function that takes a "speed" parameter. When the fadeIn animation completes, the callback function will receive the speed value and log it to the console. This allows you to pass relevant information or variables to the callback function, enabling you to customize the behavior or perform specific actions based on the completion of the operation. Passing arguments to callback functions enhances the flexibility and extensibility of your code, enabling you to create more versatile and reusable functions in jQuery.