大量并发ajax调用及处理方法
我有一个网页,在加载时,需要从服务器获取大量 JSON 数据以动态填充各种内容。特别是,它更新了大型数据结构的一部分,我从中导出了数据的图形表示。
所以它在 Chrome 中运行得很好;然而,Safari 和 Firefox 似乎受到了一些影响。在查询大量 JSON 请求后,浏览器变得缓慢且无法使用。我假设这是由于所述数据结构的迭代相当昂贵。这是一个有效的假设吗?
如何在不更改查询语言的情况下减轻这种情况,使其成为单次提取?
我正在考虑应用一个队列来限制并发 Ajax 查询的数量(因此也限制对数据结构的并发更新的数量)...有什么想法吗?有用的指点吗?其他建议?
I have a web page which, upon loading, needs to do a lot of JSON fetches from the server to populate various things dynamically. In particular, it updates parts of a large-ish data structure from which I derive a graphical representation of the data.
So it works great in Chrome; however, Safari and Firefox appear to suffer somewhat. Upon the querying of the numerous JSON requests, the browsers become sluggish and unusable. I am under the assumption that this is due to the rather expensive iteration of said data structure. Is this a valid assumption?
How can I mitigate this without changing the query language so that it's a single fetch?
I was thinking of applying a queue that could limit the number of concurrent Ajax queries (and hence also limit the number of concurrent updates to the data structure)... Any thoughts? Useful pointers? Other suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在浏览器端 JS 中,创建一个围绕 jQuery.post() 的包装器(或您正在使用的任何方法)
将请求附加到队列中。
还创建一个函数“queue_send”,它实际上会调用 jQuery.post() 传递整个队列结构。
在服务器上创建一个名为“queue_receive”的代理函数,该函数将 JSON 重播到服务器接口,就好像它来自浏览器一样,将结果收集到单个响应中,然后发送回浏览器。
浏览器端queue_send_success()(queue_send的成功处理程序)必须解码此响应并填充您的数据结构。
这样,您应该能够将初始化流量减少到一个实际请求,并且还可以合并网站上的一些其他请求。
In browser-side JS, create a wrapper around jQuery.post() (or whichever method you are using)
that appends the requests to a queue.
Also create a function 'queue_send' that will actually call jQuery.post() passing the entire queue structure.
On server create a proxy function called 'queue_receive' that replays the JSON to your server interfaces as though it came from the browser, collects the results into a single response, sends back to browser.
Browser-side queue_send_success() (success handler for queue_send) must decode this response and populate your data structure.
With this, you should be able to reduce your initialization traffic to one actual request, and maybe consolidate some other requests on your website as well.
我会尝试:
魔术师的答案也很好 - 但我不确定如果它符合您的定义“无需更改查询语言,使其成为单次提取” - 它将避免重新设计现有逻辑。
I'd try:
Magicianeer's answer is also good - but I'm not sure if it fits your definition of "without changing the query language so that it's a single fetch" - it would avoid re-engineering existing logic.