SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been using with great success the

$(document).ready(function() { 
    FunctionName();
});

in a SharePoint WebPart code snippet to fire off COSM CAML queries using JavaScript to build various html outputs from a SharePoint list. Works great. But now I need to run three functions when the page loads. So I used the used the following but only the first function runs. I can switch positions of the function names but the first one will run. Thought it would be a no brainer. Silly me. Thoughts?

$(document).ready(function() { 
    FunctionName1();
});
$(document).ready(function() { 
    FunctionName2();
});
$(document).ready(function() { 
    FunctionName3();
});
share|improve this question

Document ready event gets fired when the DOM get loaded. This doesn't necessarily guarantee that the JS files required for SharePoint JSOM is already loaded.

The best way is

 SP.SOD.executeFunc('sp.js', 'SP.ClientContext', readyFunction);

function readyFunction() {
    Function1();
    Function2();
    Function3();
}
share|improve this answer

If I understand your question correctly, you are asking how to stack asynchronous calls so that the second function only executes when the first is complete.

Look into jQuery deferreds, they allow you to do exactly this: api jquery com

Take this as an example:

_spBodyOnLoadFunctionNames.push("Init");

function Init() {
    Func1().done(function() {
        Func2().done(function() {
            Func3().done(function() {
                // All three functions with asynchronous calls now completed in order successfully
            });
        });
    });
}

function Func1() {
    var dfd = $.Deferred();
    $.ajax({
        url: //url,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {
            // Do something with data if you want to
            dfd.resolve();
        },
        error: function (data) {
            // Do something with data if you want to
            dfd.reject();
        }
    });
    return dfd;
}

function Func2() {
    // Exactly as Func 1
}

function Func3() {
    // Exactly as Func1
}

First Func1 will execute. Only when the ajax call has returned successfully will Func2 then execute. If Func1's ajax call failed Func2 will not be executed. This is what the deferred object's resolve and reject functions ensure.

As you'll see in the link above, if you want Func2 to execute regardless of the result of Func1's ajax call then use the always method in place of done.

So the effect of the above code will be first Func1 begins and completes, then Func2 begins and completes, then Func3 begins and completes.

If you have a requirement to wait for the ajax calls from Func1 and Func2 to return successfully (in any order) before making the ajax call in Func3, you can do this:

function Init() {
    $.when(Func1(), Func2()).done(function() {
        Func3();
    });
}

If there is no dependency on one function running before another then your Init function just needs to look like this:

function Init() {
    Func1();
    Func2();
    Func3();
}

With this, although each function will be executed in order, the result of each one's asynchronous ajax calls could return in any order.

A note about _spBodyOnLoadFunctionNames. This is SharePoint's version of jQuery's $(document).ready. It ensures all SharePoint related elements are loaded.

share|improve this answer
    
Not my original intent, but I can see how this can be very useful. Current I using the functions to pull from a list to build a JumboTron, then pull from three separate lists to build a left, middle and right column. I'll try these ideas out and see how they work. Thanks – PianoMan yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.