如何一次执行几个Ajax请求 [英] How to perform Ajax requests, a few at a time

查看:124
本文介绍了如何一次执行几个Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定它在JavaScript中是否可行,所以我想我会问。 :)

I am not really sure it is possible in JavaScript, so I thought I'd ask. :)

假设我们有100个请求要完成,并希望加快速度。

Say we have 100 requests to be done and want to speed things up.

我是什么想做的是:


  • 创建一个循环,启动前5个ajax调用

  • 等待直到它们全部返回(成功 - 调用函数来更新dom /错误) - 不确定如何使用全局计数器?

  • 重复直到所有请求都完成。

考虑到浏览器JavaScript不支持线程,我们可以利用 async 功能吗?那?
你认为它会起作用,或者在JavaScript中存在固有的问题吗?

Considering browser JavaScript does not support thread, can we "exploit" the async functionality to do that? Do you think it would work, or there are inherent problems doing that in JavaScript?

推荐答案

我会说,Dancrumb的评论是这个问题的答案,但无论如何......

I'd say, the comment from Dancrumb is the "answer" to this question, but anyway...

目前的浏览器确实限制了HTTP请求,所以你甚至可以轻松地启动所有100个立即请求,浏览器将尽可能快地发送这些请求,但仅限于相当数量的并行请求。

Current browsers do limit HTTP requests, so you can even easily just start all 100 request immediately, and the browser will take care of sending those requests as fast as possible, but limited to a decent number of parallel requests.

所以,立即启动它们并且信任浏览器。

So, just start them all immediately and trust on the browser.

然而,这可能会在未来发生变化(随着最终用户互联网带宽的增加和技术进步,浏览器发送的并行请求数量也会增加)。

However, this may change in the future (the number of parallel requests that a browser sends increases as end-user internet bandwidth increases and technology advances).

编辑:您还应该在javascript上下文中思考和阅读异步的含义..异步这里只是意味着你放弃控制关于某些系统其他部分的内容。所以发送异步请求只是意味着你告诉浏览器这样做!您无法控制浏览器,只需告诉发送该请求,并通知我结果

EDIT: you should also think and read about the meaning of "asynchronous" in a javascript context.. asynchronous here just means that you give up control about something to some other part of a system. so "sending" an async request just means, that you tell the browser to do so! you do not control the browser, you just tell it to send that request and please notify me about the outcome.

这篇关于如何一次执行几个Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆