ajax”;“忙”;指示符,但仅适用于较长的请求

ajax "busy" indicator but only for longer requests

本文关键字:适用于 请求 指示符 ajax      更新时间:2024-02-03

是否可以指定显示繁忙指示器的时间?

我的繁忙指示器代码很简单:

jQuery.ajaxSetup( {
    beforeSend:function ()
    {
        jQuery( "#busy-indicator" ).show();
    }, complete:function ()
    {
        jQuery( "#busy-indicator" ).hide();
    }
} );

但Ajax请求通常比出现指示符更快,所以我想为请求显示它,比如说至少需要1秒,这可能吗?或者你知道怎么做吗?

这就完成了任务:

var timeout; 
jQuery.ajaxSetup( {
    beforeSend:function ()
    {
        if (timeout) { clearTimeout(timeout); }
        timeout = setTimeout(function() {
            jQuery( "#busy-indicator" ).show(); 
        }, 1000);
    }, 
    complete:function ()
    {
        if (timeout) { clearTimeout(timeout); } 
        jQuery( "#busy-indicator" ).hide();
    }
});
jQuery.ajaxSetup( {
    beforeSend:function ()
    {
        $( "#busy-indicator" ).stop(1,0).hide().delay(1000).show();
    }, 
    complete:function ()
    {
        $( "#busy-indicator" ).stop(1,0).hide();
    }
});

这也行得通吗?