Niccaman Posted June 3, 2009 Share Posted June 3, 2009 I'm a bit of a noob with Ajax. I believe i understand most of the ajax process, but i am curious as to whether i am allowed to have more than 1 simultaneous running ajax functions. Say i have an ajax operation running, then another ajax function to another file is called. This seems to cease the previous operation from finishing its processes. Say: var xmlHttp; function example1() { xmlHttp=GetXmlHttpObject(); if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return; } var url="/example.php"; url=url+"?id=1"; url=url+"&sid="+Math.random(); xmlHttp.onreadystatechange=function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getelementbyid('example').innerHTML = xmlHttp.responseText; } }; xmlHttp.open("GET",url,true); xmlHttp.send(null); } function example2() { xmlHttp=GetXmlHttpObject(); if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return; } var url="/example2.php"; url=url+"?id=1"; url=url+"&sid="+Math.random(); xmlHttp.onreadystatechange=function stateChanged2() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getelementbyid('example2').innerHTML = xmlHttp.responseText; } }; xmlHttp.open("GET",url,true); xmlHttp.send(null); } function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } Is it because im using the same variable "xmlHttp" for both of these functions??? Will they run independent of each other if i make 2 variables? because i figured they are inside their own functions and arent global.... Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 3, 2009 Share Posted June 3, 2009 Running multiple AJAX connections on one variable can be tricky. Either use more than one or wait until the first one finishes. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 5, 2009 Share Posted June 5, 2009 Or use a proven library like Dojo for XHR functionality. Quote Link to comment 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.