JQuery: Fade Out - Do Something - Fade in Pattern

It seems I regularly write jQuery template code like this:

Fade Out ==> Do Something Behind the Scenes ==> Fade In
Illustrated below:

/// <reference path="jquery-1.4.2.js" />
/// <reference path="jquery-1.4.2-vsdoc.js" />
/// <reference path="jquery.validate-vsdoc.js" />
var fade = "slow";

$(document).ready(function () {

    // Some event occurs
    $("#Trigger").change(function () {
        var id = $(this).find(":selected").val();        

        // Fade out target while I do something
        $("#Target").fadeOut(fade, function () {
            if (id != "") {

                // Do Something
                $("#Target").load(
                    "/Site/Controller/Action/"+id, null,
                    function () {

                        // Fade in Target
                        $("#Target").fadeIn(fade);
                    });
            }
        });
    });
});

This works fine, but the callback hierarchy is getting pretty deep and I'm just wondering if there is an easier way to do this or a better method that doesn't lead to so many levels of callbacks

+3
source share
2 answers

Use jQuery.queue

$("#Target")
    .fadeOut()
    .queue(function() {
        if (id != "")
            // Do Something
            $(this).load(
                "/Site/Controller/Action/"+id, null,
                $(this).dequeue
            );
        else
            $(this).dequeue();
    })
    .fadeIn()
+6
source

jQuery.queue() . , , . :

1), :

$('#label')
    .fadeOut()                     //animate element
    .queue(function() {            //do something method1
        ...your code here...
        $(this).dequeue //dequeue the next item in the queue
    })
    .queue(function (next) {          //do something method2
        ...your code here...
        next(); //dequeue the next item in the queue
    })
    .delay(5000)                   //do lots more cool stuff.
    .show("slow")
    .animate({left:'+=200'},2000)
    .slideToggle(1000)
    .slideToggle("fast")
    .animate({left:'-=200'},1500)
    .hide("slow")
    .show(1200)
    .slideUp("normal", runIt)
    .fadeIn()

2) - , , , :

var $header = $("#header");
var $footer = $("#footer");

function runIt() {
    $header.queue(function(next){
        ...do something...
        next();
        }
    $header.queue(function(next){
        functionABC(variable, next);
        })
    $footer.animate({left:'+=200'},2000);
    $("#left").slideToggle(1000);
    $(".class1").slideToggle("fast");
    $(".class2").animate({left:'-=200'},1500);
    $("whatever").delay(3000);
    $(".class3").hide("slow");
      }

    runIt();    //execute it now, or...

    $(window).load(function() {  //execute it after the page loads!
       runIt();
    })

queue: false. , , . :

function runIt() {
    $("#first").animate(
        {width: '200px'},
        {duration:2000, queue:false}
    );
    $footer.animate(
        {left:'+=200'},
        2000
    );
}

AJAX . :

AJAX .

+1

Source: https://habr.com/ru/post/1755049/