0

I have the following code:

 $("#scheduleLink").trigger("click");
 alert("text")

This is the click handler:

$("#scheduleLink").bind("click", (function () {
            loadScheduleEvent();
            $(".wrap_tabs").find("a").removeClass("active"); 
            $(this).addClass("active");
        }));

and loadScheduleEvent function:

function loadScheduleEvent() {
        var eventId = $(".movie").attr("eventId");
        var type = $(".movie").attr("type");
        $("#publicationBlockContent").load("/Publication/EventSchedule?eventId=" + eventId + "&type=" + type);
    }  

I suppose that this code work async. I want that alert("text") calls only when loadScheduleEvent is finished. How can I do this?

Thanks.

UPDATE: In fact, instead of alert("text") there is some code. And, I can't move this code to callback of $.load function.

2
  • 1
    loadScheduleEvent() is finished (almost) immediately. You want the alert when $.load() is finished. Commented Apr 20, 2012 at 12:41
  • Yes, I want when $.load is finished. But, I can't move alert(in fact there is other code) to callback of $.load function. Commented Apr 20, 2012 at 13:05

4 Answers 4

2

Use the .load Callback

$("#publicationBlockContent")
    .load("/Publication/EventSchedule?eventId=" + eventId + "&type=" + type,
          function(){alert("text");}
    );

rtm http://api.jquery.com/load/ for additional callback parameters etc.

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

1 Comment

No, no.... I have to run code locate after $("#scheduleLink").trigger("click"); because I can't move it to handler in .load.
1

If you don't want move that code replaced by alert alternatively all you can do is fire one event which triggers your behavior replaced by alert.

$("#scheduleLink").bind("click", (function () {
        loadScheduleEvent();
        $(".wrap_tabs").find("a").removeClass("active"); 
        $(this).addClass("active");
    }));



$(window).bind("scheduleComplete", (function (event,params) {
      alert(params);
    }));

Now in loadScheduleEvent you have to trigger it.

 function loadScheduleEvent() {
    var eventId = $(".movie").attr("eventId");
    var type = $(".movie").attr("type");
    $("#publicationBlockContent").load("/Publication/EventSchedule?eventId=" + eventId + "&type=" + type,function(){$(window).trigger("scheduleComplete");});
}

And at last when you what this sequence execute you have to trigger only click event

$("#scheduleLink").trigger("click");

Also if you dont want scheduleComplete event to be exposed for window you can bind it with your scheduleLink also and get that behavior scoped and specific!!!...

Comments

0
$("#publicationBlockContent").load"/Publication/EventSchedule?eventId=" + eventId + "&type=" + type,    
function (responseText, textStatus, XMLHttpRequest) {     
if (textStatus == "success") {          
alert("success")
}
 if (textStatus == "error") {
      alert("failed")
 }
} 

Comments

0

Looks like there are 3 solutions:

1 - Create a function, and invoke it from a callback on .load

function loadComplete() {
    alert("text");
}

$("#scheduleLink")
.on("click", function () {

    $("#publicationBlockContent")
    .load("/my-url", function () {
        loadComplete();
    });

})
.trigger("click");

2 - Bind a custom event to $("#scheduleLink") called "loadComplete", and trigger it from a callback on .load

$("#scheduleLink")
.on("loadComplete", function () {
    alert("text");
})
.on("click", function () {

    $("#publicationBlockContent")
    .load("/my-url", function () {
        $("#scheduleLink").trigger("loadComplete");
    });

})
.trigger("click");

3 - If you don't need a .load, you can use the promise object supported by $.ajax, $.get and $.post

$("#scheduleLink")
.on("click", function () {

    return $.get("/my-url", function () {
        $("#scheduleLink").trigger("loadComplete");
    });

})
.trigger("click");

var promise = $("#scheduleLink").triggerHandler("click");
promise && promise.done(function () {
    alert("click");
});

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.