Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show spinner and disable page while waiting for ajax request

I have a project in which I use MVC C#, with Bootstrap and FontAwesome.

My objective is to show a spinner and disable the page while waiting for an ajax request.

So far, I have successfully acomplished this goal with the following code:

HTML:

<div id='ajax_loader' style="position: fixed; left: 50%; top: 50%; display: none;">
    <img src="~/Images/ajax-loader.gif">
</div>

JS:

function blablabla() {
            //show spinner, disable page
            var modal = $('<div>').dialog({ modal: true });
            modal.dialog('widget').hide();
            $('#ajax_loader').show();

            $.ajax({
                type: "Get",
                url: url,
                success: function (result) {
                   alert("success! " + result);
                },
                error: function(result) {
                    alert("error!" + result);
                },
                complete: function () {
                    //back to normal!
                    $('#ajax_loader').hide();
                    modal.dialog('close');
                }
            });
        }

Now, this code works, I have the page disabled and I show a spinner. However, this spinner is also grayed out, and I do not want that to happen.

How can I prevent this bug ?

like image 293
Flame_Phoenix Avatar asked Apr 08 '15 10:04

Flame_Phoenix


People also ask

How to add spinner in ajax call?

1). Create a spinner image or load it from ajax loader. 2). Place the image in our code where you want to show.

How to show loading spinner in jQuery?

Answer: Use the ajaxStart() and ajaxStop() MethodWhile working with Ajax, showing a loading spinner or displaying a message with some animation like "Loading... Please Wait" is popular way to indicate the user that Ajax request is in progress.


2 Answers

So, after a long search, I came up with my own solution:

This is the modal with the spinning wheel, or progress bar, or anything you want to. It is in a partial file called "_WaitModal"

<div class="modal hide" id="pleaseWaitDialog" data-backdrop="static" data-keyboard="false">
    <div class="modal-header">
        <h1>Please Wait</h1>
    </div>
    <div class="modal-body">
        <div id="ajax_loader">
            <img src="~/Images/ajax-loader.gif" style="display: block; margin-left: auto; margin-right: auto;">
        </div>
    </div>
</div>

I use @Html.Partial("_WaitModal") in the view to integrate it (but not show it).

Then, when I want to show it, I use:

$('#pleaseWaitDialog').modal();

and to hide it:

$('#pleaseWaitDialog').modal('hide');

I hope this helps other people as well!

like image 51
Flame_Phoenix Avatar answered Oct 13 '22 12:10

Flame_Phoenix


try this..

Html

<button>Click Me</button>
<div class="ajax_loader hidden"><i class="fa fa-spinner fa-spin"></i></div>

CSS

body{
    position:relative;
}
.hidden{
    display:none;
}
.ajax_loader{
    position:absolute;
    width:100%;
    height:100%;
    left:0;
    top:0;
    background:rgba(0,0,0,.5);
}
.ajax_loader i{
    position:absolute;    
    left:50%;
    top:50%;
}

Script

$("button").click(function () {
    $(".hidden").show();
});

Fiddle Demo

like image 44
Aru Avatar answered Oct 13 '22 13:10

Aru