How to have AJAX trigger the browser's loading indicator

前端 未结 6 1975
臣服心动
臣服心动 2020-12-05 06:54

I\'m making an ajax-enabled lab scheduling program, and some of the ajax operations aren\'t exactly quick.

In Gmail, when you go to your inbox, send a message, etc.

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 07:11

    I believe you want something like this.

    I have added an API to pull geo location of github.com (not related, but I think it serves the purpose of sample API).

    It's simple JavaScript and JQuery code to show/hide the loading indicator while making the AJAX request.

    $('#btn').click(function() {
      $('#overlay').show();
      $.ajax('https://freegeoip.net/json/github.com').then(function(response) {
        $('#overlay').hide();
        $('#country').html(response.country_name);
      });
    });
    body {
      padding: 0;
      margin: 0;
    }
    
    #overlay {
      position: fixed;
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      top: 0;
      background-color: rgba(255, 255, 255, 0.7);
    }
    
    .plus {
      display: flex;
      margin: 0 auto;
    }
    
    
    
    
    Country:

提交回复
热议问题