SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard aaron.martone's Avatar
    Join Date
    Feb 2003
    Location
    Florida, USA
    Posts
    2,322
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Forcing sequential custom functions with jQuery

    Is it at all possible to force synchronous and sequential calls to my custom javascript functions with jQuery?

    Code:
    doThis();
    doThat();
    
    function doThis() {
    // This takes a while, but we only want to do doThat() once doThis() is finished
    }
    
    function doThat() {
    // This takes much less time, but we only want it to fire off when doThis() is done.
    }
    With jQuery animation, there's a nice callback functionality where the animation waits until completion before the callback is called. But is there a way I can do similar for custom functions?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by aaron.martone View Post
    Is it at all possible to force synchronous and sequential calls to my custom javascript functions with jQuery?
    Yes there is.

    Code javascript:
    function doThis(callback) {
        // This takes a while, but we only want to do doThat() once doThis() is finished
     
        // now fire off the callback
        if (typeof callback === 'function') {
            callback();
        }
    }
     
    function doThat() {
        // This takes much less time, but we only want it to fire off when doThis() is done.
    }
     
    doThis(doThat);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Wizard aaron.martone's Avatar
    Join Date
    Feb 2003
    Location
    Florida, USA
    Posts
    2,322
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Paul.

    Wow, very neat. It's not jQuery (which, I admit, would have been the preferred method of invocation), but this will more than suffice for what I need. Thank you very much for your help!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •