jQuery.active 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3148225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery.active function
提问by RyanP13
I was trying to find some more information on the following jQuery function:
我试图找到有关以下 jQuery 函数的更多信息:
jQuery.active
It is described to test the number of active connections to a server and will evaluate true when the number of connections is zero.
它被描述为测试到服务器的活动连接数,当连接数为零时将评估为真。
I could not find any information about this function on the jQuery site and was wondering if anyone knew where I could.
我在 jQuery 站点上找不到有关此函数的任何信息,想知道是否有人知道我可以在哪里找到。
回答by Nick Craver
This is a variable jQuery uses internally, but had no reason to hide, so it's there to use. Just a heads up, it becomes jquery.ajax.active
next release. There's no documentation because it's exposedbut not in the official API, lots of things are like this actually, like jQuery.cache
(where all of jQuery.data()
goes).
这是 jQuery 在内部使用的变量,但没有理由隐藏,因此可以使用。只是提醒一下,它会成为jquery.ajax.active
下一个版本。没有文档,因为它是公开的,但不在官方 API 中,实际上很多事情都是这样,例如jQuery.cache
(所有内容jQuery.data()
)。
I'm guessing here by actualusage in the library, it seems to be there exclusively to support $.ajaxStart()
and $.ajaxStop()
(which I'll explain further), but they only care if it's 0 or not when a request starts or stops. But, since there's no reason to hide it, it's exposed to you can see the actual number of simultaneousAJAX requests currently going on.
我在这里猜测库中的实际使用情况,它似乎专门支持$.ajaxStart()
和$.ajaxStop()
(我将进一步解释),但他们只关心在请求开始或停止时它是否为 0。但是,由于没有理由隐藏它,因此您可以看到当前正在进行的并发AJAX 请求的实际数量。
When jQuery starts an AJAX request, this happens:
当 jQuery 启动 AJAX 请求时,会发生这种情况:
if ( s.global && ! jQuery.active++ ) {
jQuery.event.trigger( "ajaxStart" );
}
This is what causes the $.ajaxStart()
event to fire, the number of connections just went from 0 to 1 (jQuery.active++
isn't 0 after this one, and !0 == true
), this means the first of the current simultaneousrequests started. The same thing happens at the other end. When an AJAX request stops (because of a beforeSend
abort via return false
or an ajax call complete
function runs):
这就是导致$.ajaxStart()
事件触发的原因,连接数刚刚从 0 变为 1(jQuery.active++
在此之后不是 0,并且!0 == true
),这意味着当前并发请求中的第一个开始。同样的事情发生在另一端。当AJAX请求停止(因为一个beforeSend
通过中止return false
或AJAX调用complete
函数的运行):
if ( s.global && ! --jQuery.active ) {
jQuery.event.trigger( "ajaxStop" );
}
This is what causes the $.ajaxStop()
event to fire, the number of requests went down to 0, meaning the last simultaneousAJAX call finished. The other global AJAX handlersfire in there along the way as well.
这就是导致$.ajaxStop()
事件触发的原因,请求数下降到 0,这意味着最后一次同时进行的AJAX 调用已完成。在全球其他AJAX处理火在那里沿途为好。
回答by Sean Bannister
For anyone trying to use jQuery.active with JSONP requests (like I was) you'll need enable it with this:
对于任何试图将 jQuery.active 与 JSONP 请求一起使用的人(就像我一样),您需要通过以下方式启用它:
jQuery.ajaxPrefilter(function( options ) {
options.global = true;
});
Keep in mind that you'll need a timeout on your JSONP request to catch failures.
请记住,您的 JSONP 请求需要超时才能捕获失败。