0

My team is developing a web application, for a particular element we have a click event which is defined in two js files.

$("#manage_teams_register_btn").live("click",function(){

// codes here

})

now I am appending html elements in one js file and in the other i am getting data from db and appending it. Is it possible to set some priority in these live click functions so that one gets called ahead of the other!?

I cant use solutions like putting all contents of one live click into a function and calling it from live click defined in other js or any other solutions, i need to put up the priority inside these live click functions. Is there a way we can do this??

3

3 Answers 3

4

This model might helpful

$("#manage_teams_register_btn").live("click",function(){
        function1();
        function2();
});

function function1(){

}

function function2(){

}

Maintain only one click event otherwise it horriblly kills the readability od code.

Sign up to request clarification or add additional context in comments.

6 Comments

Probably you have to add setTimeout after ajax calling function, to ensure that the 2nd function runs after the ajax finished. functionAjax(); setTimeout(function(){ nonAjaxFunction(); });
Yes,What @VovaLando told is true.Keep in mind the Asynchronous strategy also.
if function 1 involves async tasks call function 2 within function 1 (success callback) to ensure priority. Set time out is not always reliable.
@2-Stroker Yes asynchronous in the sense,Success call back only.If you go for set timeout,How much time you specify in timeout ??You never knows when the request completes :)
that the reason I used setTimeout wo 'time' specifying because, setTimeout wo time waits for flow to be clear before it runs his content stackoverflow.com/questions/9083594/…
|
1

The jQuery observer part uses queue to reveal events attached. So if you write

$("#manage_teams_register_btn")
    .on("click.firstEventNamespace", function () {
        console.log("Hello");
    })
    .on("click.secondEventNamespace", function () {
        console.log("World!");
    });

You will get "HelloWorld!" in your console like no problem =)

But if you attach your events from different files, you should consider using some patterns, like abstract factory for your controls.

Comments

0

Event handlers in jQuery are always executed in the exact order that they were bound. Ref.

You just need to make sure that the different codes that bind the events are binding them on the same element, and that they are executed (included) in the order that you want the handlers to be executed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.