简体   繁体   中英

How can I delay an ajax call in jQuery?

Is there a way to delay an ajax request from executing until an animation finished?

var showAjaxLoader = function(selector) {
    $("." + selector).fadeToggle("slow");
    $("." + selector).parent().html('<img src="ajax-loader.gif" />').fadeIn();
};

$(".add, .delete")
    .bind("ajax:beforeSend", function(event, data, status, xhr) {
        showAjaxLoader(this.className);
});

Basically, the response of the request will replace the lay contents, but I don't want this to happen until the effect took place. Otherwise the image will just pop up with no effect...

U can use a callback methode for when the animation is finished:

$("p").fadeToggle("slow", function () {
   $("div").append("<div>finished animation</div>");
   // Here you can put your ajax request
});

Just use callback, as in the .fadeToggle() , .fadeIn() and .fadeOut() documentation. Make AJAX call within this callback.

And I believe you do not want to make request even when fading out that box (at least I don't see the reason here), but only when fading in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM