jjacquay712 Posted August 18, 2009 Share Posted August 18, 2009 Yes you read correctly, I need to call the function outside of the function from within the function. Here is my problem, I have an ajax function that needs to be run continuously when the previous one completes. The problem is that the ajax function returns control to the browser immediately, so a while loop would crash the browser. If I tried to call the function from itself, when the ready state changes to 4, the call would be recursive and eventually crash the browser. So my only choice would be to try to call the function's self from outside of the function code. I'm not sure this is even possible. Does anyone have a suggestion on how I could do this? Sorry if this is confusing. Thanks, John Quote Link to comment Share on other sites More sharing options...
rhodesa Posted August 18, 2009 Share Posted August 18, 2009 you shouldn't have a problem calling the function again on readystate 4...can you show some code of what you have so far? Quote Link to comment Share on other sites More sharing options...
jjacquay712 Posted August 18, 2009 Author Share Posted August 18, 2009 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript"> function ajaxObj() { if ( window.XMLHttpRequest ) { return new XMLHttpRequest(); } else if ( window.ActiveXObject ) { return new ActiveXObject("Microsoft.XMLHTTP"); } } function ajaxRequest() { var xmlhttp = ajaxObj(); xmlhttp.onreadystatechange = function() { if ( xmlhttp.readyState == 4 ) { alert(xmlhttp.responseText); ajaxRequest(); //This is recursive and bad } } xmlhttp.open("GET", "test.php", true); xmlhttp.send(null); } </script> </head> <body onload="ajaxRequest()"> </body> </html> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted August 18, 2009 Share Posted August 18, 2009 Ok...so there is nothing "wrong" with your code. Doing a recursive AJAX call is perfectly acceptable as long as it's handled properly. What are you trying to accomplish? If you explain your goal, I can probably give you tips on what you should/shouldn't do. Quote Link to comment Share on other sites More sharing options...
jjacquay712 Posted August 18, 2009 Author Share Posted August 18, 2009 Well the problem is, that the code is recursive. It keeps calling itself without ever ending. None of the called functions ever end. This will cause the browser to run out of memory and crash eventually. Quote Link to comment Share on other sites More sharing options...
jjacquay712 Posted August 18, 2009 Author Share Posted August 18, 2009 I think what im trying to accomplish is the equivalent of a goto statement. Instead of calling the function from inside of the function, I could use a goto to jump to outside of the function so it wouldn't be recursive. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted August 18, 2009 Share Posted August 18, 2009 Can you please just explain what you are trying to do? This part of the code: xmlhttp.onreadystatechange = function() { if ( xmlhttp.readyState == 4 ) { alert(xmlhttp.responseText); ajaxRequest(); //This is recursive and bad } } is an anonymous function that gets run when the ajax call is complete. after you alert the response, what do you want to happen next? do you not want to do the AJAX call again? Quote Link to comment Share on other sites More sharing options...
jjacquay712 Posted August 18, 2009 Author Share Posted August 18, 2009 It's just an example, but I was wanting to implement a chat script. And that would be the function to update the chat. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted August 18, 2009 Share Posted August 18, 2009 see...THAT is why i asked The best method that I am aware of, which is pure javascript, uses this exact code. But, instead of immediately returning, test.php should wait until there is new data to send back. So, test.php should look something like: <?php //After 30 seconds, end the script, even if there is nothing new //This helps keep the browser's connection from timing out $timeout = 30; //This for loop will run until 30 seconds has passed //After each loop, it will also sleep for 0.1 seconds to keep the server load down for($start = time();time() < $start + $timeout;usleep(100000)){ //Here is where you check for new stuff if($stuff_found){ //Print it out then break the loop break; } } //Now, either the loop timed out or data was found ?> This way, the client's AJAX is still in a loop, but it's only doing a call when needed Make sense? Quote Link to comment Share on other sites More sharing options...
jjacquay712 Posted August 18, 2009 Author Share Posted August 18, 2009 Yeah, that's the way I am doing it with PHP. Another way I was thinking of doing it with JavaScript is something like this: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript"> var ready = true; function ajaxObj() { if ( window.XMLHttpRequest ) { return new XMLHttpRequest(); } else if ( window.ActiveXObject ) { return new ActiveXObject("Microsoft.XMLHTTP"); } } function init() { setInterval("pollVariable()", 300); } function pollVariable() { if ( ready ) { ajaxRequest(); ready = false; } } function ajaxRequest() { var xmlhttp = ajaxObj(); xmlhttp.onreadystatechange = function() { if ( xmlhttp.readyState == 4 ) { alert(xmlhttp.responseText); ready = true; } } xmlhttp.open("GET", "test.php", true); xmlhttp.send(null); } </script> </head> <body onload="init();"> </body> </html> it checks every 300ms if the request has completed, and if it has, then start a new one. Which method do you think is better? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted August 18, 2009 Share Posted August 18, 2009 no need to keep checking...the function will run when it's done... 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.