大量并发ajax调用及处理方法

发布于 2024-11-04 18:35:37 字数 315 浏览 0 评论 0原文

我有一个网页,在加载时,需要从服务器获取大量 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜深人未静 2024-11-11 18:35:37

在浏览器端 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.

天涯沦落人 2024-11-11 18:35:37

特别是,它更新了较大数据结构的部分内容,我从中导出数据的图形表示。

我会尝试:

  • 对传入的响应进行排队,然后更新一次结构
  • 隐藏表示不可见,直到响应位于

魔术师的答案也很好 - 但我不确定如果它符合您的定义“无需更改查询语言,使其成为单次提取” - 它将避免重新设计现有逻辑。

in particular, it updates parts of a largish data structure from which i derive a graphical representation of the data.

I'd try:

  • Queuing responses as they come in, then update the structure once
  • Hiding the representation invisible until the responses are in

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文