Member Avatar for feoperro

Hi,

Does anyone know how to control the sequence of JQuery $(document).ready functions?

For example:

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

Only if this returns true then do:

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

Thanks

There's no straightforward way of knowing the order the functions will run. I would assume the last function to be appended will likely run last, but depending on how jQuery loops through the functions they could be called in reverse.

Even if you could predict the order of the functions, there's no built-in method for cancelling the loop (maybe try return false;, although I doubt it will do anything).

Your problem seems fairly trivial, why not instead call the second ready function inside your if statement within the first ready function?

There's no straightforward way of knowing the order the functions will run. I would assume the last function to be appended will likely run last, but depending on how jQuery loops through the functions they could be called in reverse.

Even if you could predict the order of the functions, there's no built-in method for cancelling the loop (maybe try return false;, although I doubt it will do anything).

Your problem seems fairly trivial, why not instead call the second ready function inside your if statement within the first ready function?

Member Avatar for feoperro

Good point, sorry about that...

You see, the first function is actually a validate:

$(document).ready(function() {
    $("#register_form").validate({
        rules: {
            username: {
                required: true,
                minlength: 3
            }
        },
        messages: {
            username: {
                required: "Please specify your name",
                minlength: "Your username should be at least 3 characters"
            }
        }

    });
});

Now I don't want to run the 2nd function (an ajax call to the back end, to check username availability) until the 1st function has no problems (there is input, and minlength is at least 2 chars).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.