Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing loading image with getJSON

Tags:

json

jquery

ajax

I want to use a loading image while retrieving data through AJAX with getJSON. I have been looking around but haven't yet found a proper way of doing this. What is the best way to do this?

$.getJSON('file.php', function(json) {
    $.each(json, function() {

    // Retrieving data from json...

    });
});
like image 385
Elephant Avatar asked Dec 18 '11 10:12

Elephant


2 Answers

Show spinner before getJson call and then hide after response is parsed

 $(".someSpinnerImage").show();
    $.getJSON('file.php', function(json) {
       $.each(json, function() {
          // Retrieving data from json...

       });
       $(".someSpinnerImage").hide();  
    });
like image 69
heads5150 Avatar answered Sep 19 '22 11:09

heads5150


you can configure for global use, it will be internally called whenever an ajax call is made.

$.ajaxStart(function() {
    $("img#loading").show();
});

$.ajaxComplete(function() {
    $("img#loading").hide();
});
like image 45
dku.rajkumar Avatar answered Sep 18 '22 11:09

dku.rajkumar