Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ajax";“忙”;指示器,但仅适用于较长的请求_Javascript_Jquery - Fatal编程技术网

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

Javascript ajax";“忙”;指示器,但仅适用于较长的请求,javascript,jquery,Javascript,Jquery,是否可以指定显示忙碌指示器的时间 我的忙指示器代码非常简单: jQuery.ajaxSetup( { beforeSend:function () { jQuery( "#busy-indicator" ).show(); }, complete:function () { jQuery( "#busy-indicator" ).hide(); } } ); 但Ajax请求通常比显示指示器更快,所以我想为至少需要1秒的请求

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

我的忙指示器代码非常简单:

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

这也行吗?

在beforeSend回调中启动一个超时,它将在一段时间后显示加载程序,在完整的回调中取消超时并隐藏加载程序。经过一些测试后,我将添加到代码中:
if(timeout){clearTimeout(timeout);}
同样在beforeSend的开头,两个连续的ajax请求造成了一点超时:)这是一种更具jquery风格的方法:)。请解释一下
.stop(1,0)
?好的。stop(1,0)告诉jQuery清除此元素的动画队列,这样,如果多次调用ajax函数,动画将不会运行多次(动画之间有1秒的延迟)。因此,在ajax调用之前,我们确保没有其他来自上一个调用的动画正在运行,然后我们隐藏指示器(如果它可见),然后以1秒的延迟显示它。
jQuery.ajaxSetup( {
    beforeSend:function ()
    {
        $( "#busy-indicator" ).stop(1,0).hide().delay(1000).show();
    }, 
    complete:function ()
    {
        $( "#busy-indicator" ).stop(1,0).hide();
    }
});