Calling one jQuery function only after another function has run

Sometimes it may be nessessary to force one jQuery function to run only after another has finished. To do this you need to use the jQuery deferred method.

Here is a simple example of how to do it:

    var functionOne = function() {

      var r = $.Deferred();

      // Do your whiz bang jQuery stuff here
      console.log('Function One');

      return r;

    };

    var functionTwo = function() {

      // Do your whiz bang jQuery stuff here
      console.log('Function Two');

    };

    // Now call the functions one after the other using the done method
    functionOne().done( functionTwo() );

Source: http://stackoverflow.com/questions/12116505/wait-till-a-function-is-finished-until-running-another-function

Advertisements

3 thoughts on “Calling one jQuery function only after another function has run”

  1. Hi
    Ive a situation where i want to refresh the page then css add class but its adding class while the page is still in refresh mode so the class doesn’t attach, how can i wait till its full refreshed?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s