简体   繁体   English

我可以检测浏览器的网络活动是否空闲吗?

[英]Can I detect if a browser's network activity is idle?

In an angularJS app there is quite a big of latency between the user and the server (also bandwidth may be limited), so when the user requests a new page, it has to wait about 2-500ms for it to load.在 angularJS 应用程序中,用户和服务器之间存在相当大的延迟(带宽也可能有限),因此当用户请求新页面时,它必须等待大约 2-500 毫秒才能加载。

I'm thinking of "preloading" model data, templates, scripts in the background, but intend to do so ONLY if there is no page requested by the user.我正在考虑在后台“预加载”模型数据、模板、脚本,但仅在用户没有请求页面的情况下才打算这样做。 If the user requests one specific page I'd like to stop the preloading process and load the resources explicitly requested by the user.如果用户请求一个特定页面,我想停止预加载过程并加载用户明确请求的资源。

So my question boils down to:所以我的问题归结为:

  • Is there a way to make an ajax request "ONLY IF" there is no network activity?有没有办法“仅当”没有网络活动时发出 ajax 请求?
  • Is there a way to "pause" currently running ajax requests?有没有办法“暂停”当前正在运行的ajax请求? or或者
  • Is there a way to prioritize ajax requests?有没有办法优先处理ajax请求?

Thanks,谢谢,

Seems like https://nicedoc.io/pastelsky/network-idle-callback will be helpful for you.似乎https://nicedoc.io/pastelsky/network-idle-callback会对您有所帮助。

It uses serviceWorker to observe network calls, and dispatch event when it senses network idle.它使用 serviceWorker 来观察网络调用,并在感知到网络空闲时调度事件。

npm install network-idle-callback

Usage 用法

Sounds like requestidlecallback is what you're looking for.听起来requestidlecallback就是你要找的。 There's also a package built on top that exposes an event you can listen to: https://github.com/choojs/on-idle还有一个构建在顶部的包,用于公开您可以收听的事件: https : //github.com/choojs/on-idle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM