แสดงภาพขณะโหลดขณะที่ $ .ajax ดำเนินการ


116

ฉันแค่สงสัยว่าจะแสดงภาพที่บ่งชี้ว่าคำขอ async กำลังทำงานอยู่ได้อย่างไร ฉันใช้รหัสต่อไปนี้เพื่อดำเนินการร้องขอ async:

$.ajax({
  url: uri,
  cache: false,
  success: function(html){
    $('.info').append(html);
  }
});

ความคิดใด ๆ ?

คำตอบ:


255

แน่นอนคุณสามารถแสดงก่อนส่งคำขอและซ่อนหลังจากเสร็จสิ้น:

$('#loading-image').show();
$.ajax({
      url: uri,
      cache: false,
      success: function(html){
        $('.info').append(html);
      },
      complete: function(){
        $('#loading-image').hide();
      }
    });

ฉันมักจะชอบวิธีแก้ปัญหาทั่วไปมากกว่าในการผูกเข้ากับเหตุการณ์ ajaxStart และ ajaxStop ระดับโลกวิธีนี้จะปรากฏขึ้นสำหรับเหตุการณ์ ajax ทั้งหมด:

$('#loading-image').bind('ajaxStart', function(){
    $(this).show();
}).bind('ajaxStop', function(){
    $(this).hide();
});

30
เริ่มต้น jQuery 1.9 ควรแนบเหตุการณ์ AJAX เข้ากับdocumentไฟล์. ดูstackoverflow.com/questions/2275342/…
Simone

62

ใช้ ajax object ของ beforeSend และฟังก์ชั่นที่สมบูรณ์ ควรแสดง gif จากข้างใน beforeSend เพื่อให้พฤติกรรมทั้งหมดถูกห่อหุ้มไว้ในวัตถุเดียว ระวังการซ่อน gif โดยใช้ฟังก์ชันสำเร็จ หากคำขอล้มเหลวคุณอาจยังต้องการซ่อน gif ในการดำเนินการนี้ให้ใช้ฟังก์ชัน Complete จะมีลักษณะดังนี้:

$.ajax({
    url: uri,
    cache: false,
    beforeSend: function(){
        $('#image').show();
    },
    complete: function(){
        $('#image').hide();
    },
    success: function(html){
       $('.info').append(html);
    }
});

ขอบคุณสำหรับตัวอย่างข้อมูลง่ายๆ ประหยัดเวลา @jEremyB
Anahit DEV

นี่เป็นวิธีแก้ปัญหาที่มีประโยชน์และเป็นสากลมาก ขอบคุณ.
Eryk Wróbel

20

รหัส HTML:

<div class="ajax-loader">
  <img src="{{ url('guest/images/ajax-loader.gif') }}" class="img-responsive" />
</div>

รหัส CSS:

.ajax-loader {
  visibility: hidden;
  background-color: rgba(255,255,255,0.7);
  position: absolute;
  z-index: +100 !important;
  width: 100%;
  height:100%;
}

.ajax-loader img {
  position: relative;
  top:50%;
  left:50%;
}

รหัส JQUERY:

$.ajax({
  type:'POST',
  beforeSend: function(){
    $('.ajax-loader').css("visibility", "visible");
  },
  url:'/quantityPlus',
  data: {
   'productId':p1,
   'quantity':p2,
   'productPrice':p3},
   success:function(data){
     $('#'+p1+'value').text(data.newProductQuantity);
     $('#'+p1+'amount').text("₹ "+data.productAmount);
     $('#totalUnits').text(data.newNoOfUnits);
     $('#totalAmount').text("₹ "+data.newTotalAmount);
  },
  complete: function(){
    $('.ajax-loader').css("visibility", "hidden");
  }
});

}

8

โดยทั่วไป "รูปภาพ" ที่คนทั่วไปจะแสดงในระหว่างการโทร ajax คือ gif แบบเคลื่อนไหว เนื่องจากไม่มีวิธีกำหนดเปอร์เซ็นต์ที่สมบูรณ์ของคำขอ ajax ดังนั้น gif แบบเคลื่อนไหวที่ใช้จึงเป็นสปินเนอร์ที่ไม่แน่นอน นี่เป็นเพียงภาพที่ทำซ้ำแล้วซ้ำอีกเหมือนลูกบอลวงกลมที่มีขนาดแตกต่างกัน ไซต์ที่ดีในการสร้างสปินเนอร์ที่ไม่แน่นอนของคุณเองคือhttp://ajaxload.info/


6

ฉันคิดว่านี่อาจจะดีกว่าถ้าคุณมีการโทรเป็นจำนวน $ .ajax

$(document).ajaxSend(function(){
    $(AnyElementYouWantToShowOnAjaxSend).fadeIn(250);
});
$(document).ajaxComplete(function(){
    $(AnyElementYouWantToShowOnAjaxSend).fadeOut(250);
});

บันทึก:

หากคุณใช้ CSS องค์ประกอบที่คุณต้องการแสดงในขณะที่ ajax กำลังดึงข้อมูลจากโค้ดส่วนหลังของคุณจะต้องเป็นเช่นนี้

AnyElementYouWantToShowOnAjaxSend {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh; /* to make it responsive */
    width: 100vw; /* to make it responsive */
    overflow: hidden; /*to remove scrollbars */
    z-index: 99999; /*to make it appear on topmost part of the page */
    display: none; /*to make it visible only on fadeIn() function */
}

2
นี่ควรเป็นคำตอบที่ยอมรับได้เนื่องจากเป็นเรื่องทั่วไป!
Rotimi

3

ฉันชอบBlockUIปลั๊กอินนี้มาโดยตลอด: http://jquery.malsup.com/block/

ช่วยให้คุณสามารถบล็อกองค์ประกอบบางอย่างของเพจหรือทั้งเพจในขณะที่คำขอ ajax ทำงานอยู่


1

ก่อนที่คุณจะโทรให้แทรกรูปภาพการโหลดใน div / span ที่ใดที่หนึ่งจากนั้นในฟังก์ชันความสำเร็จให้ลบรูปภาพนั้นออก หรือคุณสามารถตั้งค่าคลาส css เช่นการโหลดที่อาจมีลักษณะเช่นนี้

.loading
{
    width: 16px;
    height: 16px;
    background:transparent url('loading.gif') no-repeat 0 0;
    font-size: 0px;
    display: inline-block;
}

จากนั้นกำหนดคลาสนี้ให้กับช่วง / div และล้างในฟังก์ชันความสำเร็จ



0

คุณสามารถเพิ่ม ajax start และ complete event ซึ่งใช้ได้เมื่อคุณคลิกที่ปุ่ม event

 $(document).bind("ajaxSend", function () {
            $(":button").html('<i class="fa fa-spinner fa-spin"></i> Loading');
            $(":button").attr('disabled', 'disabled');
        }).bind("ajaxComplete", function () {
            $(":button").html('<i class="fa fa-check"></i> Show');
            $(":button").removeAttr('disabled', 'disabled');
        });

0
  1. สร้างองค์ประกอบโหลดเช่นองค์ประกอบที่มี id = example_load
  2. ซ่อนตามค่าเริ่มต้นโดยเพิ่ม style = "display: none;"
  3. ตอนนี้แสดงโดยใช้ jquery show element function เหนือ ajax ของคุณ

    $('#example_load').show(); $.ajax({ type: "POST", data: {}, url: '/url', success: function(){ // Now hide the load element $('#example_load').hide(); } });


-1

**strong text**Set the Time out to the ajax calls
function testing(){
    
    $("#load").css("display", "block");
    setTimeout(function(){
    $.ajax({
             type: "GET",

          
             url:testing.com,
            
             async: false,
             
             success : function(response){
           
             alert("connection established");

              
            },
            complete: function(){
            alert("sended");
            $("#load").css("display", "none");
         
           },
            error: function(jqXHR, exception) {
                       alert("Write error Message Here");
                  },


       });
     },5000);


  }
  .loader {
    border: 16px solid #f3f3f3;
    border-radius: 50%;
    border-top: 16px solid #3498db;
    width: 120px;
    height: 120px;
    -webkit-animation: spin 2s linear infinite; /* Safari */
    animation: spin 2s linear infinite;
  }
  
  /* Safari */
  @-webkit-keyframes spin {
    0% { -webkit-transform: rotate(0deg); }
    100% { -webkit-transform: rotate(360deg); }
  }
  
  @keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
  }
<div id="load" style="display: none" class="loader"></div>
<input type="button"  onclick="testing()"  value="SUBMIT" >

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.