Logo
5 Aug 2012 | 2 min. (236 words)

Using jQuery callback functions

I’ve heard questions in the past regarding how to run a script after a jQuery animation or event has been completed.

The majority of these believe that you need to delay the script for a few seconds, allowing the animation to complete before the 2nd function is called. For example:

$foo.load("MyWebPage.html");
$foo.delay(1000).css("color", "red");

Here, the jQuery object $foo is being loaded with a web page’s content, then while this is happening a delay of 1000 milliseconds is set, before the css colour is changed to red.

This is not the best method, and can cause various problems. The load time of MyWebPage.htm will be a variable amount of time. Therefore it is not possible to estimate how long the delay needs to be set for.

Setting the delay to 1000 milliseconds could result in the css being set before or after the content has loaded, and not when the content has loaded.

Fortunately, a lot of jQuery methods also provide a callback function. This allows us to run a function when the current function has completed.

The script below will run the load function on $foo, and once this has finished executing, the css function will then be executed.

$foo.load("MyWebPage.html", function(){
    $(this).css("color", "red");
});

This means you don’t need to guess how long the first function will take to complete, and also ensures that the 2nd function is consistently ran precisely after the 1st has completed.

callback-function delay javascript jquery
Twitter Facebook

How to prevent self-closing elements in XSLT

…

Strongly typed Stored Procedure names in C#

…

Related Links

  • LinkedIn
  • Twitter: @curtcode
  • Stack Overflow
  • GitHub: @curtiscde

Stack Exchange

profile for Curtis on Stack Exchange, a network of free, community-driven Q&A sites
Follow @curtiscde

Recent Posts

  • My terminal setup and commands I use on a daily basis when working with GitHub
  • Displaying latest posts on your GitHub profile
  • Using Codecov within a monorepo
  • Displaying Strava stats using webhooks & GitHub Actions
  • Filtering duplicate data points on Chart.js
Theme Bleak by zutrinken Published with Hugo
Menu