How to show processing animation / spinner during ajax request?

前端 未结 7 1622
礼貌的吻别
礼貌的吻别 2020-12-23 23:55

I want a basic spinner or processing animation while my AJAX POST is processing. I\'m using JQuery and Python. I looked at the documentation but can\'t figure out exactly

相关标签:
7条回答
  • 2020-12-24 00:30

    The AJAX process starts when you run the $.ajax() method, and it stops when the 'complete' callback is run. So, start your processing imagery/notification right before the $.ajax() line, and end it in the 'complete' callback.

    ajaxStart and ajaxStop handlers can be added to any elements, and will be called whenever ajax requests start or stop (if there are concurrent instances, start only gets called on the first one, stop on the last to go). So, it's just a different way of doing global notification if you had, for example, a status spinner somewhere on the page that represents any and all activity.

    0 讨论(0)
  • 2020-12-24 00:34

    I wrote a blog post about how to do this on a generic document level.

    // prepare the form when the DOM is ready 
    $(document).ready(function() { 
    
      // Setup the ajax indicator
      $('body').append('<div id="ajaxBusy"><p><img src="images/loading.gif"></p></div>');
    
      $('#ajaxBusy').css({
        display:"none",
        margin:"0px",
        paddingLeft:"0px",
        paddingRight:"0px",
        paddingTop:"0px",
        paddingBottom:"0px",
        position:"absolute",
        right:"3px",
        top:"3px",
         width:"auto"
      });
    });
    
    // Ajax activity indicator bound to ajax start/stop document events
    $(document).ajaxStart(function(){ 
      $('#ajaxBusy').show(); 
    }).ajaxStop(function(){ 
      $('#ajaxBusy').hide();
    });
    
    0 讨论(0)
  • 2020-12-24 00:38

    you can set global ajax loading icon handler using here #ajxLoader takes your loading icon

       $( document ).ajaxStart(function() {
            $("#ajxLoader").fadeIn();
        });
    
        $( document ).ajaxComplete(function() {
            $("#ajxLoader").fadeOut();
        });
    
    0 讨论(0)
  • 2020-12-24 00:41
    $(function() {
            $('.error').hide();
            $("#checkin-button").click(function() {
                    var mid = $("input#mid").val();
                    var message = $("textarea#message").val();
                    var facebook = $('input#facebook').is(':checked');
                    var name = $("input#name").val();
                    var bgg_id = $("input#bgg-id").val();
                    var thumbnail = $("input#thumbnail").val();
                    var dataString = 'mid=' + mid + '&message=' + message + '&facebook=' + facebook + '&name=' + name + '&bgg_id=' + bgg_id + '&thumbnail=' + thumbnail;
                    $.ajax({
                            type : "POST",
                            url : "/game-checkin",
                            data : dataString,
                            beforeSend : function() {
                             $('#preloader').addClass('active');
                            },
                            success : function(badges) {
                                $('#preloader').removeClass('active');
                                $('#checkin-form').html("<div id='message'></div><div id='badges'></div>");
                                $('#message').html("<h2><img class=\"check-mark\" src=\"/static/images/check-mark.png\"/>You are checked in!</h2>");
                                $.each(badges, function(i, badge) {
                                        $('#badges').append("<h2>New Badge!</h2><p><img class='badge' src='" + badge.image_url + "'><span class='badge-title'>" + badge.name + "</span></p>");
                                    });
                            },
                            complete : function() {
                                $('#preloader').removeClass('active');                      
                            }
                        });
                    return false;
                });
        });
    

    #preloader{
     background: url(staticpreloader.gif);
    }
    .active {
     background: url(activepreloader.gif);
    }
    
    0 讨论(0)
  • 2020-12-24 00:42

    The best method I have found, assuming you are populating a present but empty field is to have a .loading class defined with background-image: url('images/loading.gif') in your CSS. You can then add and remove the loading class as necessary with jQuery.

    0 讨论(0)
  • 2020-12-24 00:45

    If you're using jQuery 1.5 you could do that nicely, unobtrusively and generically with a prefilter. Let's make a very simple plugin for this:

    (function($) {
    
        var animations = {};
    
        $.ajaxPrefilter(function( options, _, jqXHR ) {
            var animation = options.animation && animations[ options.animation ];
            if ( animation ) {
                animation.start();
                jqXHR.then( animation.stop, animation.stop );
            }
        });
    
        $.ajaxAnimation = function( name, object ) {
            if ( object ) {
                animations[ name ] = object;
            }
            return animations[ name ];
        };
    
    })( jQuery );
    

    You install an animation as follows:

    jQuery.ajaxAnimation( "spinner" , {
        start: function() {
            // code that starts the animation
        }
        stop: function() {
            // code that stops the animation
        }
    } );
    

    then, you specify the animation in your ajax options:

    jQuery.ajax({
        type: "POST",
        url: "/game-checkin",
        data: dataString,
        animation: "spinner",
        success: function() {
            // your success code here
        }
    });
    

    and the prefilter will ensure the "spinner" animation is started and stopped when needed.

    Of course, that way, you can have alternative animations installed and select the one you need per request. You can even set a default animation for all requests using ajaxSetup:

    jQuery.ajaxSetup({
        animation: "spinner"
    });
    
    0 讨论(0)
提交回复
热议问题