0

In Firefox, I have a synchronous request that happens (it must be sync or the later JS will not run correctly) now in the XMLHttpReq function it basically changes one of the DIV's contents to a tiny loading logo (turning cog) so you can see progress... In FF this works 100% async or sync... Chrome and IE no such luck..?

Can anyone help me or explain why please? Im guessing it something to do with them disabling ALL other processes but if this is so how can i get this to work OR evaluate and JS file after this original XMLHttpReq has loaded?

Trott
  • 66,479
  • 23
  • 173
  • 212
Ash
  • 8,583
  • 10
  • 39
  • 52
  • 1
    How can we possibly help if you don't include your relevant code? – jfriend00 Aug 19 '11 at 22:36
  • 1
    The solution is usually not to use synchronous requests. If asynchronous requests break your JavaScript, it might be feasible to restructure the JavaScript – Pekka Aug 19 '11 at 22:37
  • @Pekka - what im asking is if this is the case, how can i restructure it so the desired piece of code that must be loaded after the XMLHttpReq is definitely loaded after? @ jfriend00 - Also this isnt a direct code related question, therefore no code examples are necessary. – Ash Aug 19 '11 at 22:44
  • 2
    @Ash the usual way of restructuring is putting all code that depends on data from the request into the "request finished" event (or have it called from there). It might be worth using a framework like jQuery for easier handling... You'll have to decide whether it's worth doing all this for the effect you desire. There may be another way to keep the GIF spinning but I can't think of an easy one. – Pekka Aug 19 '11 at 22:47
  • It's absolutely a code related question. YOUR code has a problem and without us seeing it, we could only offer wild guesses for what the issue is. Now you're asking how to restructure your code without showing us your code. I'm voting to close since you won't include relevant information. – jfriend00 Aug 19 '11 at 22:48
  • 1
    @jfriend00 - As you can see below the answers i got were perfect without any code, i needed to know (as i said in the question) what behaviours the browser has and is there a way around this. So if you cant answer it please just back off, i asked for help from people who understood my question not who wanted a pointless argument. – Ash Aug 20 '11 at 10:14
  • @Pekka - I am actually using the jQuery library and thats the reason i need a specific piece of code to load last because the jqery wont have the elements it needs to run on until the async request is done, how could i handle this with jQuery? Does this have an AJAX callback function like the guy below mentioned? Thank you, ash (: – Ash Aug 20 '11 at 10:17
  • @Ash - if you explained what you're REALLY trying to accomplish in your question, folks could have easily told you to stop using synchronous ajax and use the success callback from an async ajax call to start whatever you're trying to do when the ajax call is done. But you included no such information or code in your question. If you want to provide very little actual info about what you're really trying to do in your question, people can't advise you how to solve your overall problem. This could have been solved in minutes. Your loss, not ours. – jfriend00 Aug 20 '11 at 14:42
  • 1
    @jfriend00 - It was solved in minutes, so no loss for me. Also just as i said kindly back off if you cannot answer without petty squabbling. Cheers. – Ash Aug 21 '11 at 14:43

2 Answers2

1

Is there any reason to use a synchronous XMLHttpRequest?

Synchronous calls tie up your browser until the transaction is finished, which is why it is advised against.

You should look at sequencing your asynchronous calls. Meaning... the problem isn't with your asynchronous call, it's in your reason why it must be sync or the later JS will not run correctly. Setting a completed variable in the AJAX callback is pretty simple to do, which the other problematic JS can check before executing.

Community
  • 1
  • 1
vol7ron
  • 40,809
  • 21
  • 119
  • 172
  • Thank you both or your answers, @vol7ron - Please can you give an example of how to do an AJAX callback? That sounds like its exactly what i need, thank you! Ash! :) – Ash Aug 20 '11 at 10:12
  • Ash: the callback is already built into your AJAX request. You should have a response handler function attached to the `readystatechange` portion of the AJAX object. The function should check the HTTP status of the returned message (eg. if it's `200` do something). In that `if` block, you should set your global variable, or call other JS functions/code – vol7ron Aug 20 '11 at 14:32
  • Thank you for your help, all solved now with your guidance. I have created a function for running the AJAX and used eval to run the code with the callback after the AJAX query is complete :) Thank you all again! – Ash Aug 21 '11 at 14:48
  • @Ash: you may want to open another question, or get on a debugging/help forum. `eval` is useful, but it could also be dangerous, depending on what you're doing. Regardless, what you might want to do is have a function defined elsewhere and call that function from within the callback. – vol7ron Aug 21 '11 at 15:32
0

Frame.js is designed to enable synchronous requests without hanging up the browser.

Frame(function(next){
    startImageRotation();
    next();
});
Frame(function(next){
    $.ajax('someUrl.api', {
        ...
        complete: next
    });
});
Frame(function(next, response){
    // do stuff with ajax response
});

The other commenters are correct in saying that truly synchronous AJAX request are mostly useless because of poor browser support. Frame.js was designed as a solution to that problem. Best luck.

BishopZ
  • 6,269
  • 8
  • 45
  • 58