otester Posted August 13, 2010 Share Posted August 13, 2010 Seems that only one works at a time, each one when called (button press on page) individually (blank out all other functions) works and if I leave them unblanked only the last function works.. How can I get them all to display their data instead of just one of them? Each function is like this, draws data from a MySQL database: function loadFuntion_x() { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("function_x").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","/scripts/x.php",true); xmlhttp.send(); } jQuery code that is meant to call all functions: $(document).ready(function(){ $('#function_all').click(function() { loadFunction_1(); loadFunction_2(); loadFunction_3(); loadFunction_4(); loadFunction_5(); loadFunction_6(); loadFunction_7(); loadFunction_8(); }); }); Any help would be greatly appreciated, Thanks, otester Quote Link to comment Share on other sites More sharing options...
RussellReal Posted August 13, 2010 Share Posted August 13, 2010 there is no proper way of doing this, HOWEVER, you can hack it together with timeouts , timeouts will not halt other script execution (unless there is an alert) setTimeout('loadFunction_1()',5); setTimeout('loadFunction_2()',5); setTimeout('loadFunction_3()',5); setTimeout('loadFunction_4()',5); Quote Link to comment Share on other sites More sharing options...
otester Posted August 13, 2010 Author Share Posted August 13, 2010 there is no proper way of doing this, HOWEVER, you can hack it together with timeouts , timeouts will not halt other script execution (unless there is an alert) setTimeout('loadFunction_1()',5); setTimeout('loadFunction_2()',5); setTimeout('loadFunction_3()',5); setTimeout('loadFunction_4()',5); Thanks for post but got a response off another forum this morning, turns out you need to do this: ... if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari [color=red]var[/color] xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 [color=red]var[/color] xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } ... Quote Link to comment Share on other sites More sharing options...
RussellReal Posted August 13, 2010 Share Posted August 13, 2010 there is no proper way of doing this, HOWEVER, you can hack it together with timeouts , timeouts will not halt other script execution (unless there is an alert) setTimeout('loadFunction_1()',5); setTimeout('loadFunction_2()',5); setTimeout('loadFunction_3()',5); setTimeout('loadFunction_4()',5); Thanks for post but got a response off another forum this morning, turns out you need to do this: ... if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari [color=red]var[/color] xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 [color=red]var[/color] xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } ... ohhh.. I thought you meant executing functions at the same time not 1 after the other.. you know you can also do this... function loadFuntion() { var xmlhttp = ''; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp; } function getAndPopulate(xmlhttp,num,pageToGet) { xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("function_"+num).innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET",pageToGet,true); xmlhttp.send(); } getAndPopulate(loadFunction(),1,'/scripts/1.php'); getAndPopulate(loadFunction(),2,'/scripts/2.php'); getAndPopulate(loadFunction(),3,'/scripts/3.php'); with alot less code Quote Link to comment Share on other sites More sharing options...
Alex Posted August 13, 2010 Share Posted August 13, 2010 Just because you set the timeouts to fire at the same time doesn't mean the functions will run concurrently. It will still process one after another. Quote Link to comment Share on other sites More sharing options...
otester Posted August 14, 2010 Author Share Posted August 14, 2010 ohhh.. I thought you meant executing functions at the same time not 1 after the other.. you know you can also do this... function loadFuntion() { var xmlhttp = ''; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp; } function getAndPopulate(xmlhttp,num,pageToGet) { xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("function_"+num).innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET",pageToGet,true); xmlhttp.send(); } getAndPopulate(loadFunction(),1,'/scripts/1.php'); getAndPopulate(loadFunction(),2,'/scripts/2.php'); getAndPopulate(loadFunction(),3,'/scripts/3.php'); with alot less code Damn this does save a lot of code, but I have other buttons which need to call scripts individually. How do you call them properly? I tried: $(document).ready(function(){ $('#function_x').click(function() { getAndPopulate(loadFunction(),1,'/scripts/function_x.php'); }); }); But it didn't work, replacing the code with an alert message works so I know every apart from this: getAndPopulate(loadFunction(),1,'/scripts/function_x.php'); EDIT: Hmm seems button might actually be broken I'll re-edit soon. REDIT: Nope button does work just got mixed with another one Quote Link to comment Share on other sites More sharing options...
otester Posted August 14, 2010 Author Share Posted August 14, 2010 Edit stopped working so basically I still can't call it Quote Link to comment Share on other sites More sharing options...
trq Posted August 14, 2010 Share Posted August 14, 2010 Why are so many people misusing jQuery these days? Are you aware that jQuery provides Ajax functionality that will be allot more reliable than what you have written. Quote Link to comment Share on other sites More sharing options...
otester Posted August 14, 2010 Author Share Posted August 14, 2010 Why are so many people misusing jQuery these days? Are you aware that jQuery provides Ajax functionality that will be allot more reliable than what you have written. Didn't know, I'm new to the whole jQuery scene, only found out it existed when I had a mootools conflict so I had to build my own slideshow. Any help to the last script or anything else would be great! Quote Link to comment Share on other sites More sharing options...
otester Posted August 14, 2010 Author Share Posted August 14, 2010 Turns out you got to put the functions outside of the document.ready bit: function loadFunction() { var xmlhttp = ''; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp; } function getAndPopulate(xmlhttp,num,pageToGet) { xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("function_"+num).innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET",pageToGet,true); xmlhttp.send(); } $(document).ready(function(){ $('#function_button').click(function() { getAndPopulate(loadFunction(),1,'/scripts/1.php'); getAndPopulate(loadFunction(),2,'/scripts/2.php'); getAndPopulate(loadFunction(),3,'/scripts/3.php'); }); }); 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.