0

I want to show a full-screen loader when my AJAX request is working. How can I do that?

$("#send").click(function() {
    if ($("#ccval").val() == $("#contactcaptcha").text()) {
        var id = $("#contactcaptcha").attr("data-id");
        var cemail = $("#cemail").val();

        $.post(base_url + "index.php/myad/getphonenumber", {
            uniqueid: id,
            emailaddress: cemail
        }, function() {
            alert('email is sent');
        })
    } else {
        $("#ccval").val("");
        $("#ccval").attr("placeholder", "invalid captcha");
    }
})
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Shah Rushabh
  • 147
  • 4
  • 16

6 Answers6

2
$(function () {
    $(document).ajaxStart(function () {
        $("#Loader").show();
    });

    $(document).ajaxStop(function () {
        $("#Loader").hide();       
    });

    $(document).ajaxError(function () {
        $("#Loader").hide();       
    }); 
});

The other answers require you to duplicate the show/hide code for each ajax call. This is fine if you only have 1 call, but I assume you have several calls in your app.

My code above will show your loader during all ajax requests. I suggest adding it to your layout page, or putting it in a common js file.

#Loader is up to you. In my site, I style it as a fixed position element with a centered progress bar. Here is some basic styling you can use as a starting point.

#Loader {
    position: fixed;
    width: 100%;
    height: 100%;
    display: none;
    background-color: #fff;
    opacity: 0.6;
    z-index: 99999;
}
Kev
  • 2,656
  • 3
  • 39
  • 63
  • I'm using the short version of document ready above. See https://stackoverflow.com/questions/6004129/document-ready-shorthand. You could also write it as an IIFE - see https://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript – Kev Nov 14 '16 at 12:51
2

Have a div#ajax-loader somewhere in your document and set it's styles:

/* You can style this further - this is just an example */
#ajax-loader {
    position: absolute;
    width: 100%;
    height: 100%;
    text-align: center;
    color: #fff;
    background: rgba(0, 0, 0, 0.5);
    padding-top: 35%;
    display: none;
}

And place it in your document as:

<div id="ajax-loader">Loading content...</div>

Now, change your jQuery code into:

$("#send").click(function() {
    if ($("#ccval").val() == $("#contactcaptcha").text()) {
        var id = $("#contactcaptcha").attr("data-id");
        var cemail = $("#cemail").val();

        // notice this line
        $('#ajax-loader').show();
        // this will set display: none; -> display: block; for this element

        $.post(base_url + "index.php/myad/getphonenumber", {
            uniqueid: id,
            emailaddress: cemail
        }, function() {
            alert('email is sent');

            // notice this line too
            $('#ajax-loader').hide();
            // just toggle the view vice-versa...
        })
    } else {
        $("#ccval").val("");
        $("#ccval").attr("placeholder", "invalid captcha");
    }
});

EDIT: Also, an answer below posted by Kev is highly suggested.

Zlatan Omerović
  • 3,863
  • 4
  • 39
  • 67
0

Well, first you need to make whatever image overlay you want. That's just CSS/HTML and up to your presentation. But here's one way to do it:

$(trigger).on('whatever', function(){ 
...
   $(loading_selector).show()
   $.ajax({ ..., 
            success:function(data) { 
               $(loading_selector).hide();
                  }
            });
 });

The problem here is, the loading selector will not hide if your ajax fails, so you'll need to add in some code to catch ajax errors and hide the loading pane in that logic block as well.

rob
  • 2,119
  • 1
  • 22
  • 41
0

Use the show loading indicator before you call Ajax and hide it afte the success or error case or you can use the complete handler of jQuery ajax

    $("#send").click(function() {
   if ($("#ccval").val() == $("#contactcaptcha").text()) {
    var id = $("#contactcaptcha").attr("data-id");
    var cemail = $("#cemail").val();
    //Start showing loading indicator
    showLoadingIndicator();
    $.post(base_url + "index.php/myad/getphonenumber", {
        uniqueid: id,
        emailaddress: cemail
    }, function() {
        hideLoadingIndicator();
        alert('email is sent');
    })
} else {
    hideLoadingIndicator();
    $("#ccval").val("");
    $("#ccval").attr("placeholder", "invalid captcha");
}})});
Devesh
  • 4,500
  • 1
  • 17
  • 28
  • @ShahRushabh i hope you will add the logic to showLoadingIndicator and hideLoadingIndicator which will show the div with zindex like 1000 and width and height as 100% – Devesh Nov 11 '16 at 17:40
  • I could not understand which will show the div with zindex like 1000 and width and height as 100% can you please add it in code. – Shah Rushabh Nov 11 '16 at 17:50
0

Show it before request is sent and hide when it's done.

$('#loader').show();
$.post('example.php', function() {
    alert('success');
}).always(function() {
    alert('finished');
    $('#loader').hide();
});
Kostya Shkryob
  • 2,344
  • 17
  • 24
0

Using $.ajax I think could be easier:

$.ajax({
  url: base_url + "index.php/myad/getphonenumber",
  beforeSend: function(xhr){
     //show load },
  data: { uniqueid: id, emailaddress: cemail }
  }).done(function(d){
    alert('email is sent');
  }).always(function(){
     //remove load
  })
M. A. Cordeiro
  • 469
  • 8
  • 14