s0c0 Posted August 7, 2007 Share Posted August 7, 2007 When I make more than one XMLHTTPRequest inside a javascript function I get errors and exceptions. So I have to encapsulate other XMLHTTPRequest functions inside the setTimeout function, to pause the functions time of execution. Is this a valid way of handling such a problem? Is this really just some lowly hack? Is there a better way to do what I am attempting? Please advise. Quote Link to comment https://forums.phpfreaks.com/topic/63766-solved-ajax-question-making-mulitple-xmlhttprequests-at-the-same-time/ Share on other sites More sharing options...
448191 Posted August 7, 2007 Share Posted August 7, 2007 When using aSync requests, all bets on timing are off. The key is to distinguish between responses and be independent of the order they are received. Quote Link to comment https://forums.phpfreaks.com/topic/63766-solved-ajax-question-making-mulitple-xmlhttprequests-at-the-same-time/#findComment-317802 Share on other sites More sharing options...
roopurt18 Posted August 7, 2007 Share Posted August 7, 2007 I'd call it a lowly hack in that it will not prevent you from running into problems. Also, you must remember that most browsers will only hold two connections to a server open at a time. You have two options here. 1) Implement a Javascript priority queue. Instead of enacting the request right away, place it on the queue to be executed later when a request is available. You can only ever have two requests happening at once, so bear that in mind before allowing the queue to launch another request. Also, you should implement age-based priority to prevent a low-priority request from sitting in the queue forever. 2) Reduce the number of AJAX requests. I had a situation in my own Javascript program where it would send a request to the server, wait for the response, and based on the response send another request to the server. The second request was dependent on the first request, so I used a synchronous request in the first request, which would lock the UI until it returned. Eventually I realized I only needed one request. Example of two requests (pseudo code) request1 = send_sync_ajax_request() if(request1 == ONE_VALUE){ request2 = send_async_ajax_request() }else{ request2 = send_other_async_ajax_request() } alert( request2 ); Here we have two AJAX requests when we really only need one. Instead of performing the logic of testing the result of request1 on the client, why not just test the result on the server and invoke the second request from the server? Reducing requests down to 1 (pseudo code) // Javascript request = send_ajax_request(); alert( request ); // PHP Script on the server $result1 = perform some action based on $_POST / $_GET parameters if($result1 == ONE_VALUE){ $result2 = perform one thing }else{ $result2 = perform another thing } $JSON = Array( "result1" => $result1, "result2" => $result2 ); echo Array2JSON($JSON); A real world example would be the user enters a coupon code at a shop; if the coupon is valid you return a list of items it applies to, if the coupon is invalid you return a list of items that are still bargains and similar to the items the coupon would have applied to. You must validate the coupon on the server so the client can't bypass it. So with two requests you'd do the validation, then send another request based on the validation. But really you could send the coupon code, determine if it's valid, and return the proper list from the server right from the beginning, thus using a single request. Quote Link to comment https://forums.phpfreaks.com/topic/63766-solved-ajax-question-making-mulitple-xmlhttprequests-at-the-same-time/#findComment-317821 Share on other sites More sharing options...
s0c0 Posted August 7, 2007 Author Share Posted August 7, 2007 I think you're correct roopurt. I should have one xmlhttprequest trigger multiple method calls server-side and just return more data at once to prevent multiple requests. Damnit, I'm sick of fixing things, this is what happens when management sets strict unobtainable deadlines that I told them could not be met in the first place and they do not allow for proper testing of the application...ARRRRGGGGHHHHHHH!!!!!!! Working at home tonight...ROUND 10...FIGHT! Quote Link to comment https://forums.phpfreaks.com/topic/63766-solved-ajax-question-making-mulitple-xmlhttprequests-at-the-same-time/#findComment-317951 Share on other sites More sharing options...
448191 Posted August 8, 2007 Share Posted August 8, 2007 One request dependent on another: you can do that without using a sync request. You can use setTimeOut to continue to check if the first response has been received, until it has, then you stop the timeout function and do the second request. The UI never freezes and the dependency is maintained. Quote Link to comment https://forums.phpfreaks.com/topic/63766-solved-ajax-question-making-mulitple-xmlhttprequests-at-the-same-time/#findComment-318255 Share on other sites More sharing options...
roopurt18 Posted August 8, 2007 Share Posted August 8, 2007 Yah, but then you have to possibly introduce an extra variable as a flag into your script. Easier if you can just do more work on the server IMO, but I realize that's not always an option. Quote Link to comment https://forums.phpfreaks.com/topic/63766-solved-ajax-question-making-mulitple-xmlhttprequests-at-the-same-time/#findComment-318535 Share on other sites More sharing options...
448191 Posted August 8, 2007 Share Posted August 8, 2007 Naturally if you can avoid multiple requests, you should definitely do so. In any case, I recommend to try to avoid synchronous requests at all times. Just for correctness: my previous post said SetTimeOut, I meant SetInterval. Pardon... Quote Link to comment https://forums.phpfreaks.com/topic/63766-solved-ajax-question-making-mulitple-xmlhttprequests-at-the-same-time/#findComment-318656 Share on other sites More sharing options...
roopurt18 Posted August 8, 2007 Share Posted August 8, 2007 I agree about avoiding synchronous requests, however I have used them in minor places when they're expected to be quick. One place where I'll usually use a synchronous request is on saving data. I don't want the client to click off the page during a save so that they miss the status message for the request. I could create an iframe and post the results into there so that even if they leave the page they're notified of the save status; but I doubt my boss would want me to take the time to implement that and then go back and upgrade the few areas that need it. Bleh! Quote Link to comment https://forums.phpfreaks.com/topic/63766-solved-ajax-question-making-mulitple-xmlhttprequests-at-the-same-time/#findComment-318672 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.