Show loading image while $.ajax is performed
JavascriptJqueryCssJavascript Problem Overview
I am just wondering how to show an image that indicates that the async request is running. I use the following code to perform a async request:
$.ajax({
url: uri,
cache: false,
success: function(html){
$('.info').append(html);
}
});
Any ideas?
Javascript Solutions
Solution 1 - Javascript
You can, of course, show it before making the request, and hide it after it completes:
$('#loading-image').show();
$.ajax({
url: uri,
cache: false,
success: function(html){
$('.info').append(html);
},
complete: function(){
$('#loading-image').hide();
}
});
I usually prefer the more general solution of binding it to the global ajaxStart and ajaxStop events, that way it shows up for all ajax events:
$('#loading-image').bind('ajaxStart', function(){
$(this).show();
}).bind('ajaxStop', function(){
$(this).hide();
});
Solution 2 - Javascript
Use the ajax object's beforeSend and complete functions. It's better to show the gif from inside beforeSend so that all the behavior is encapsulated in a single object. Be careful about hiding the gif using the success function. If the request fails, you'll probably still want to hide the gif. To do this use the Complete function. It would look like this:
$.ajax({
url: uri,
cache: false,
beforeSend: function(){
$('#image').show();
},
complete: function(){
$('#image').hide();
},
success: function(html){
$('.info').append(html);
}
});
Solution 3 - Javascript
HTML Code :
<div class="ajax-loader">
<img src="{{ url('guest/images/ajax-loader.gif') }}" class="img-responsive" />
</div>
CSS Code:
.ajax-loader {
visibility: hidden;
background-color: rgba(255,255,255,0.7);
position: absolute;
z-index: +100 !important;
width: 100%;
height:100%;
}
.ajax-loader img {
position: relative;
top:50%;
left:50%;
}
JQUERY Code:
$.ajax({
type:'POST',
beforeSend: function(){
$('.ajax-loader').css("visibility", "visible");
},
url:'/quantityPlus',
data: {
'productId':p1,
'quantity':p2,
'productPrice':p3},
success:function(data){
$('#'+p1+'value').text(data.newProductQuantity);
$('#'+p1+'amount').text("₹ "+data.productAmount);
$('#totalUnits').text(data.newNoOfUnits);
$('#totalAmount').text("₹ "+data.newTotalAmount);
},
complete: function(){
$('.ajax-loader').css("visibility", "hidden");
}
});
}
Solution 4 - Javascript
I think this might be better if you have tons of $.ajax calls
$(document).ajaxSend(function(){
$(AnyElementYouWantToShowOnAjaxSend).fadeIn(250);
});
$(document).ajaxComplete(function(){
$(AnyElementYouWantToShowOnAjaxSend).fadeOut(250);
});
NOTE:
If you use CSS. The element you want to shown while ajax is fetching data from your back-end code must be like this.
AnyElementYouWantToShowOnAjaxSend {
position: fixed;
top: 0;
left: 0;
height: 100vh; /* to make it responsive */
width: 100vw; /* to make it responsive */
overflow: hidden; /*to remove scrollbars */
z-index: 99999; /*to make it appear on topmost part of the page */
display: none; /*to make it visible only on fadeIn() function */
}
Solution 5 - Javascript
The "image" people generally show during an ajax call is an animated gif. Since there is no way to determine the percent complete of the ajax request, the animated gifs used are indeterminate spinners. This is just an image repeating over and over like a ball of circles of varying sizes. A good site to create your own custom indeterminate spinner is <http://ajaxload.info/>
Solution 6 - Javascript
I've always liked the BlockUI
plugin: http://jquery.malsup.com/block/
It allows you to block certain elements of a page, or the entire page while an ajax request is running.
Solution 7 - Javascript
-
Create a load element for e.g. an element with id = example_load.
-
Hide it by default by adding style="display:none;".
-
Now show it using jquery show element function just above your ajax.
$('#example_load').show(); $.ajax({ type: "POST", data: {}, url: '/url', success: function(){ // Now hide the load element $('#example_load').hide(); } });
Solution 8 - Javascript
**you can use like this also maybe this one help you thanks **
$.ajax({
url : url,
cache : false,
beforeSend : function(){
$('#loading-image').show();
},
success: function(html){
$('#loading-image').hide();
$('.info').append(html);
},
});
Solution 9 - Javascript
Before your call either insert the loading image in a div/span somewhere and then on the success function remove that image. Alternatively you can set up a css class like loading that might look like this
.loading
{
width: 16px;
height: 16px;
background:transparent url('loading.gif') no-repeat 0 0;
font-size: 0px;
display: inline-block;
}
And then assign this class to a span/div and clear it in the success function
Solution 10 - Javascript
something like this:
$('#image').show();
$.ajax({
url: uri,
cache: false,
success: function(html){
$('.info').append(html);
$('#image').hide();
}
});
Solution 11 - Javascript
You can add ajax start and complete event, this is work for when you click on button event
$(document).bind("ajaxSend", function () {
$(":button").html('<i class="fa fa-spinner fa-spin"></i> Loading');
$(":button").attr('disabled', 'disabled');
}).bind("ajaxComplete", function () {
$(":button").html('<i class="fa fa-check"></i> Show');
$(":button").removeAttr('disabled', 'disabled');
});
Solution 12 - Javascript
Javascript
$.ajax({
url : "url",
type : "POST",
data : {},
beforeSend: function(){
$("#loader").show();
},
success : function(response){
...............
},
complete:function(data){
$("#loader").hide();
},
});
HTML (inside body)
<div id="loading"></div>
CSS
#loading {
display: block;
position: absolute;
top: 0;
left: 0;
z-index: 100;
width: 100vw;
height: 100vh;
background-color: rgba(192, 192, 192, 0.5);
background-image: url("https://i.stack.imgur.com/MnyxU.gif");
background-repeat: no-repeat;
background-position: center;}
Solution 13 - Javascript
strong textSet the Time out to the ajax calls function testing(){
$("#load").css("display", "block");
setTimeout(function(){
$.ajax({
type: "GET",
url:testing.com,
async: false,
success : function(response){
alert("connection established");
},
complete: function(){
alert("sended");
$("#load").css("display", "none");
},
error: function(jqXHR, exception) {
alert("Write error Message Here");
},
});
},5000);
}
.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 120px;
height: 120px;
-webkit-animation: spin 2s linear infinite; /* Safari */
animation: spin 2s linear infinite;
}
/* Safari */
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
<div id="load" style="display: none" class="loader"></div>