How do I load an image while waiting for AJAX response?

前端 未结 1 1744
悲&欢浪女
悲&欢浪女 2021-01-31 19:52

I was wondering if I could get some pointers. I am trying to use a loading gif while getting a response from an ajax request. The issue I am running into is that it will not dis

1条回答
  •  感情败类
    2021-01-31 20:39

    Actually you need to do this by listening ajaxStart and Stop events and binding it to the document:

    $(document).ready(function () {
        $(document).ajaxStart(function () {
            $("#loading").show();
        }).ajaxStop(function () {
            $("#loading").hide();
        });
    });
    

    $(document).ajaxStart(function() {
      $("#loading").show();
    }).ajaxStop(function() {
      $("#loading").hide();
    });
    
    $('.btn').click(function() {
      $('.text').text('');
      $.ajax({
        type: "GET",
        dataType: 'jsonp',
        url: "https://api.meetup.com/2/cities",
        success: function(data) {
          $('.text').text('Meetups found: ' + data.results.length);
        }
      });
    });
    #loading { display: none; }
    
    
    

    Loading...

    0 讨论(0)
提交回复
热议问题