模拟同步XmlHtt prequest [英] simulating a synchronous XmlHttpRequest

查看:111
本文介绍了模拟同步XmlHtt prequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过一些其他相关的问题(<一href="http://stackoverflow.com/questions/214491/pattern-for-wrapping-an-asynchronous-javascript-function-to-make-it-synchronous">http://stackoverflow.com/questions/214491/pattern-for-wrapping-an-asynchronous-javascript-function-to-make-it-synchronous &安培; <一href="http://stackoverflow.com/questions/518880/make-async-event-synchronous-in-javascript">http://stackoverflow.com/questions/518880/make-async-event-synchronous-in-javascript &放大器;可能会有更多),但我只是想一定要穷尽所有可能性。

I've read some of the other related questions (http://stackoverflow.com/questions/214491/pattern-for-wrapping-an-asynchronous-javascript-function-to-make-it-synchronous & http://stackoverflow.com/questions/518880/make-async-event-synchronous-in-javascript & there may be more), but I just want to be sure to exhaust all possibilities.

也许这是可能的转换异步XmlHtt prequest进入准同步酮使用任何的setInterval或setTimeout的?

Might it be possible to "convert" an asynchronous XmlHttpRequest into a quasi-synchronous one using either setInterval or setTimeout?

这个想法是,Ajax请求的变量将被设置,这将是一段时间循环的信号成功的基础上(即呼吁任何的setInterval或setTimeout的,并在适当的回调函数)退出。还是我从根本上误解了能力(或限制?)的setInterval和/或setTimeout的呢?

The idea being that upon success of the Ajax request a variable will be set, which will be the signal for a while loop (that has called either setInterval or setTimeout, and a callback function as appropriate) to exit. Or am I fundamentally misunderstanding the abilities (or limitations?) of setInterval and/or setTimeout?

推荐答案

这是真的,你不希望使用的setInterval和setTimeout的在你所描述的方式。你真正想要做的是刚刚获得舒适的嵌套函数,在那里你可以或多或少同步的方式写。

It's true, you don't want to use setInterval and setTimeout in the way you've described. What you really want to do is just get comfortable with nested functions, where you can write in a more or less synchronous way.

例如:

XHR.get(your_data, function()
   {
      //what you would have done "synchronously"
   });

虽然可以使用的setInterval和/或的setTimeout(通过调用的函数体再次的setTimeout)为民意调查的成功,code,这种做法是大大逊色于刚刚处理回调摆在首位,而不是轮询它。它引入了延迟,跑掉的CPU,并不能扩展到多个XHR请求,等等缺点。

While you can use setInterval and/or setTimeout (with calls to setTimeout again in the function body) to "poll" for a success code, that approach is dramatically inferior to just handling the callback in the first place instead of polling for it. It introduces latency, runs away with the CPU, and doesn't scale across multiple XHR requests, to name a few shortcomings.

XHR将调用函数完成时,这是毫无意义的运行功能要求我们做了吗?难道我们做了吗?同时。在另一方面,如果有一些你想阻塞,直到返回响应(一段动画,需要这些数据来运行,例如),这是完全可以接受的,如果包围阻塞code在周期行为声明:

XHR will call your function when it completes, it makes little sense to run a function asking "Are we done yet? Are we done yet?" in the meantime. On the other hand if there is some periodic behavior you are wanting to BLOCK until the response comes back (a piece of animation that needs that data to run, for example) it is totally acceptable to surround the blocking code in an if statement:

var tick = window.setTimeout(tock, 20);
var tock = function()
{
   if (response_done)
   {
      // dependent code
   }
   tick = window.setTimeout(tock, 20);
}
XHR.get(your_data, function() { /*Handle response*/ response_done = true; });

这篇关于模拟同步XmlHtt prequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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