[英]Showing a spinner during an AJAX request?
what is the best way to show a spinner? 展示微调器的最佳方法是什么?
I have prepared a div(id="spinner"), that should be visible during loading. 我准备了一个div(id =“spinner”),在加载过程中应该是可见的。
Do you use jQuery? 你使用jQuery吗?
If so you can use: 如果是这样,您可以使用:
ajaxStart & ajaxStop: http://docs.jquery.com/Ajax ajaxStart&ajaxStop: http ://docs.jquery.com/Ajax
For example: 例如:
$(function(){
// hide it first
$("#spinner").hide();
// when an ajax request starts, show spinner
$.ajaxStart(function(){
$("#spinner").show();
});
// when an ajax request complets, hide spinner
$.ajaxStop(function(){
$("#spinner").hide();
});
});
You can fine tune a little with a request counter that increments and decrements in case you have a lot of simultaneous requests. 您可以使用请求计数器进行微调,如果您有大量同时请求,该计数器会递增和递减。
If you don't use jQuery, check out the jQuery Source code for which events ajaxStart actually register in plain old javascript. 如果你不使用jQuery,请查看jQuery源代码,其中ajaxStart事件实际上在普通的旧javascript中注册。
HTH Alex HTH Alex
I used this in my rails app. 我在我的rails应用程序中使用了它。 This worked for me:
这对我有用:
$(document).ajaxSend(function(r, s) {
$("#spinner").show();});
$(document).ajaxStop(function(r, s) {
$("#spinner").fadeOut("fast");});
$().ajaxSend(function(r, s) {
$("#spinner").show();
});
$().ajaxStop(function(r, s) {
$("#spinner").fadeOut("fast");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.