setInterval 和 setInterval 之间的区别轮询?

发布于 2024-10-02 18:53:01 字数 145 浏览 0 评论 0原文

我想知道DOM中的setInterval()(或)setTimeout()和ajax中的轮询之间的区别。主要区别是什么?如果两者相同,为什么用两个不同的名称来标识?

AJAX 中的轮询是什么意思?

目前,有关此问题的任何链接或资源将更加感激!

I want to know the difference between setInterval() (or) setTimeout() in DOM and polling in ajax. What is the main difference? If both are same, why the identified by two different names?

What is mean by polling in AJAX?

Any links or resource about this question would be more appreciative at the moment!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

洒一地阳光 2024-10-09 18:53:01

setInterval 设置重复计时器,setTimeout 设置仅触发一次的计时器。轮询是指您反复请求某些内容而不是等待通知。有时轮询是必要的,例如,如果无法收到通知,这在 Ajax 应用程序中通常是这种情况。 setIntervalsetTimeout 都可以用来实现轮询,具体取决于您想要做什么。

如果定期向服务器发出请求,建议使用 setTimeout 而不是 setInterval。在回调中,您执行请求,等待响应,然后使用 setTimeout 设置新计时器。如果您使用 setInterval 并且请求延迟与间隔相当,那么您将面临响应无序的风险。例如,计时器触发并且您发出请求,它需要比平常更长的时间,因此在返回之前计时器会再次触发,因此您发出新的请求。现在您正在等待两个请求。最好先等待第一个请求返回,然后再执行第二个请求。

setInterval sets a repeating timer, setTimeout sets a timer that fires only once. Polling is when you repeatedly ask for something instead of waiting to be notified. Sometimes polling is necessary, for example if there's no way to be notified -- and this is often the case in Ajax applications. Both setInterval and setTimeout can be used to implement polling, depending on what you want to do.

In the case of periodically making a request to a server it's advisable to use setTimeout instead of setInterval. In the callback you do the request, wait for the response then set a new timer using setTimeout. If you use setInterval and the request latency is comparable to the interval then you risk that the responses will come out of order. For example, the timer fires and you make a request, it takes a little longer than usual so before it has returned the timer fires again, so you make a new request. Now you are waiting for two requests. It would have been better to wait for the first request to come back before doing the second.

漫雪独思 2024-10-09 18:53:01

轮询是指定期对服务器执行 ping 操作以查看是否已准备就绪。用户可能发出了一个请求,该请求将花费一些未指定的时间,但等待时间太长,因此您每隔 x 秒轮询一次服务器以查看结果是否准备就绪。

setTimeout 在指定的时间间隔后执行函数。

setInterval 每次都会重复执行一个函数。

查看 http://www.w3schools.com/js/js_timing.asp

您可以使用这两个函数来实现轮询方案,但它们绝对与轮询不同。

polling is when you periodically ping the server to see if something is ready. A user might have made a request that will take some unspecified amount of time, but too long to wait, so you poll the server every x seconds to see if the result is ready.

setTimeout executes a function after the specified interval.

setInterval repeatedly executes a function every time.

check out http://www.w3schools.com/js/js_timing.asp

You can use these two functions to implement a polling scheme, but they are definitely not the same as polling.

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