JQUERY 或 JS 有没有办法检测窗口加载的时间?基本上有任何网络活动吗?
有没有一种方法可以使用 JQUERY 或 Javascript 来检测浏览器窗口何时加载某些内容、进行 ajax 调用、加载图像等...基本上任何网络活动?
Is there a way with JQUERY or Javascript, to detect anytime the browser window is loading something, making an ajax call, loading an image, etc... Basically any network activity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
类似 jQuery 的东西(放在 HEAD 中)应该可以工作。
阅读 ajaxStart 了解更多详细信息。
Something along the lines of this jQuery (put in the HEAD) should work.
Read up on ajaxStart for more details.
我下面的答案和之前用户的答案只是尝试检查浏览器当前是否正在发出 XmlHttpRequest (即 ajaxy)请求。
您询问是否可以判断浏览器是否正在发出任何网络请求(即下载图像/css 或者可能是长时间运行的“comet”请求)。我不知道有任何 javascript API 可以告诉你这一点 - 它们可能存在,但我怀疑如果它们存在,那么它们将是特定于浏览器的[如果有人知道这个问题的答案,请参与]。显然,像 Firebug 和 dynaTrace 这样的工具可以检测网络活动,但我认为这些工具比 JavaScript 代码更深入地“挂钩”到浏览器。
话虽如此,如果您想计算 jQuery 生成的 XHR,那么 dave1010 的答案似乎是一个不错的答案。
但是我认为它很容易出现一些竞争条件问题: -
根据文档(http://api.jquery .com/ajaxStart/)
因此,如果启动了一个长时间运行的 XHR,并且在第一个 XHR 完成之前启动了另一个 XHR,则 ajaxStart 处理程序只会被调用一次。第二个 XHR 可以在第一个 XHR 之前完成并设置
loading = false
,即使第一个请求仍在进行中。查看 jQuery 1.4 源代码似乎证实了这一点。有趣的是,jQuery 1.4 有一个活动 XHR (jQuery.active) 的计数,但是文档中没有提到这一点,因此最好不要使用它(这很遗憾,因为它会让生活变得更轻松一些)。
请参阅 http://gist.github.com/277432#LID5052 查看代码在调用 ajaxStart 处理程序之前,请注意 $.active 为 0。
[我认为]“ajaxSend”全局事件处理程序在每个 XHR 之前调用。优先使用它而不是“ajaxStart”应该会有所帮助,但我们仍然需要保留活动请求的计数,而不是简单的“加载”标志。
也许像下面这样的东西会起作用?
请注意,如果任何代码在全局选项设置为 false 的情况下调用 $.ajax,这将给出错误的答案,因此很难防弹;
另请记住,activeXhrCount 仅计算由 jQuery 生成的 XHR - 您可能使用的其他库中的 XHR 将不会被计算在内。
My answer below and the previous users' answers are only attempting to check if the browser is currently making an XmlHttpRequest (i.e. ajaxy) request.
You asked if you could tell if the browser was making any network request (i.e. downloading images/css or perhaps a long running 'comet' request). I don't know of any javascript API that would tell you this - they may exist but I suspect that if they do then they would be browser specific [if anyone out there knows the answer to this please chip in]. Obviously tools like Firebug and dynaTrace can detect network activity but I think these tool "hook in" to the browser a lot deeper down than javascript code.
Having said all that, if you want to count XHRs spawned by jQuery then dave1010's answer seems like a good one.
However I think it is prone to some race condition problems : -
According to the docs (http://api.jquery.com/ajaxStart/)
So if a long running XHR was started and another one was started before the first had completed, the ajaxStart handler would only be called once. The 2nd XHR could complete before the first one and set
loading = false
, even though the first request is still in progress.Looking at the jQuery 1.4 source seems to confirm this. Interestingly, jQuery 1.4 has a count of active XHRs (jQuery.active) but this is not mentioned in the docs and so it is probably best not to use it (which is a pity because it would make life a bit easier).
See http://gist.github.com/277432#LID5052 for the code that checks to see that $.active is 0 before invoking ajaxStart handlers.
[I think] The 'ajaxSend' global event handlers are called before each and every XHR. Using this in preference to 'ajaxStart' should help but we will still need to keep a count of active requests as opposed to a simple "loading" flag.
Perhaps something like the following will work?
Be aware that this will give incorrect answers if any code calls $.ajax with the global option set to false and so is hardly bullet proof;
Also keep in mind that activeXhrCount only counts XHRs spawned by jQuery - those from other libraries that you may utilize will not be counted.
我认为你必须自己思考,
我之前做了类似的事情[就像当gmail显示加载上面时]
这个想法是关于创建一个数组然后添加/删除它并继续检查它以了解是否有一个打开的连接
代码应该如下所示
i think you have to build some think by your self,
i did something like this before [ like when gmail shows loading upper there ]
the idea is about making an array then add/remove from it and keep checking it for know if there is an open connection
the code should look like this