Jump to content

[SOLVED] Call the Function Outside Of The Function From Within the Function - LOLWUT?


jjacquay712

Recommended Posts

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

 

 

Link to comment
Share on other sites

<!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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.