English 中文(简体)
simulating a synchronous XmlHttpRequest
原标题:

I ve read some of the other related questions (Pattern for wrapping an Asynchronous JavaScript function to make it synchronous & Make async event synchronous in JavaScript & there may be more), but I just want to be sure to exhaust all possibilities.

Might it be possible to "convert" an asynchronous XmlHttpRequest into a quasi-synchronous one using either setInterval or 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?

最佳回答

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.

For example:

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

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 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; });
问题回答

If I m getting your question right, you could achieve a similar effect using the following code:

var computationInterval =
    window.setInterval(function() {
        // do a computation cycle, as if you re in the body of a while loop
    }, 100);

$.get( http://example.com/areWeThereYet , function() {
    // break the intervals (the pseudo-cycle)
    window.clearInterval(computationInterval);
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签