setInterval 和 setInterval 之间的区别轮询?
我想知道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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
setInterval
设置重复计时器,setTimeout
设置仅触发一次的计时器。轮询是指您反复请求某些内容而不是等待通知。有时轮询是必要的,例如,如果无法收到通知,这在 Ajax 应用程序中通常是这种情况。setInterval
和setTimeout
都可以用来实现轮询,具体取决于您想要做什么。如果定期向服务器发出请求,建议使用
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. BothsetInterval
andsetTimeout
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 ofsetInterval
. In the callback you do the request, wait for the response then set a new timer usingsetTimeout
. If you usesetInterval
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.轮询是指定期对服务器执行 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.