在AJAX请求期间显示微调器?

时间:2009-10-02 11:43:07

标签: javascript jquery ajax

显示微调器的最佳方法是什么?

我准备了一个div(id =“spinner”),在加载过程中应该是可见的。

3 个答案:

答案 0 :(得分:8)

你使用jQuery吗?

如果是这样,你可以使用:

ajaxStart& ajaxStop:http://docs.jquery.com/Ajax

例如:

$(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();
    });
});

您可以使用请求计数器进行微调,如果您有大量同时请求,该计数器会递增和递减。

如果你不使用jQuery,请查看jQuery源代码,其中ajaxStart事件实际上在普通的旧javascript中注册。

HTH 亚历

答案 1 :(得分:2)

我在我的rails应用程序中使用了这个。这对我有用:

$(document).ajaxSend(function(r, s) {
$("#spinner").show();});


$(document).ajaxStop(function(r, s) {
$("#spinner").fadeOut("fast");});

答案 2 :(得分:1)

$().ajaxSend(function(r, s) {
    $("#spinner").show();
});

$().ajaxStop(function(r, s) {
    $("#spinner").fadeOut("fast");
});