Jump to content

What is going on here...alert() makes or breaks function


astralaaron

Recommended Posts

function Com(url){var that = this;this.obj = null;this.url = url;this.result = null; this.HTTP = function(){  if(window.XMLHttpRequest){  that.obj = new XMLHttpRequest();  } else if(window.ActiveXObject){  that.obj =  new ActiveXObject("Microsoft.XMLHTTP");  } else {   alert('browser out dated');   return false;  }  if(!that.init()) return false;return true;}this.init = function(){  if(that.obj.readyState == 0 || that.obj.readyState == 4){  that.obj.open("GET",that.url,true);  that.obj.onreadystatechange = that.results;  that.obj.send(null);  return true;  }		} this.results = function(){  //alert('results ' + that.obj.readyState);  if(that.obj.readyState == 4){   //alert('com file' + that.obj.responseText);   that.result = that.obj.responseText;   return true;  }}						  }

Hi I was very happy when this small class worked like I wanted to so I can make my ajax calls a little easier to write... But then I commented out the alerts that I had set up in the results function() and it doesn't work anymore, the result is null.. When I removed the comment the alerts, it works again...why is the alert changing the outcome of this? (it is used like this:)

   	 var call = new Com('http://www. some web site .com/test.php');  //test file just says 123		call.HTTP(); 		switch (call.result)		{			case '123':				alert('results are all good  ' + call.result);			break; 		default: alert('connection problems ' + call.result); break; 		}

Ok I did find some one else with this problem and there was an answer to the question. Basically they are saying my code is structured wrong because the response is 'asynchronous' and doesn't come immediately. The alert delays the function long enough for the results to come in. But the way they are saying to fix it doesn't make any difference, which was to do this:

    	 that.obj.onreadystatechange = function() {	   	 if(that.obj.readyState==4) {			that.results();	   	 }		}

instead of

   	 that.obj.onreadystatechange = that.results;

I am trying to figure it out at the moment, would appreciate any help edit: I found some one saying this works but is not the correct way to fix it because it will freeze the browser until it gets the request...On this particular page I am doing I don't care of it freezes like that but the whole purpose of making that class was so I can re use it on other things too so I want to figure outhow I can re structure it and make it work with true.

//that.obj.open("GET",that.url,true);  that.obj.open("GET",that.url,false);

I am running out of ideas, I added a delay function to the Com ajax class

	this.delay = function(i)	{		i += new Date().getTime();		while (new Date() < i){}	}

so I can try this, even though it doesn't seem like a good solution.

   	 call = new Com('http://www. some web site .com/test.php');		call.HTTP();		call.delay(4000);

still after the 4 second delay, the switch goes to the default and the call.result is null when the alert(); is not there. That makes me think that it is something other than the alert pausing the functions.. because I click the alerts faster than 4 seconds

Link to comment
Share on other sites

The problem is that you're expecting the request to return results immediately. when the alerts run, they give time for the request to finish. You should always pass a callback function to deal with asynchronous requests. You should make it work like this:

var X = function() {  switch(call.result) {    case '123':	  alert('results are all good  ' + call.result);    break;    default:	  alert('connection problems ' + call.result);  }}call.HTTP(X); // The HTTP function will execute the callback "X" when the request has returned.

Otherwise, if you really expect the rest of the code to run only after the request has finished you'll have to set async to false as you were told.

Link to comment
Share on other sites

You should make it work like this:
var X = function() {  switch(call.result) {	case '123':	  alert('results are all good  ' + call.result);	break;	default:	  alert('connection problems ' + call.result);  }}call.HTTP(X); // The HTTP function will execute the callback "X" when the request has returned.

I like this option but it is not working for me. Do I need to change the HTTP function to expect X and return it? Something seems missing, the switch doesn't fire off any alert
Link to comment
Share on other sites

You'll have to manipulate call.HTTP(), of course. It looks like both your init() method and your results() method are pretty much unnecessary.

this.HTTP = function(callback) {  if(window.XMLHttpRequest){    that.obj = new XMLHttpRequest();  } else if(window.ActiveXObject){    that.obj =  new ActiveXObject("Microsoft.XMLHTTP");  } else {   alert('browser out dated');   return false;  }  that.obj.open("GET",that.url,true);  that.obj.onreadystatechange = function() {    that.result = that.obj.responseText;     callback();  }  that.obj.send(null);}

Link to comment
Share on other sites

Thank you so much that works great. there was a small problem, it was running the X function and hitting the default once and then the '123' case in the switch 2 or 3 times I added this and it works perfect

       	 if(that.obj.readyState == 4 && that.obj.status == 200)            {                callback();            }

Link to comment
Share on other sites

Oh, actualy, that would probably be better in the readystatechange event handler.
this is how I have it
 that.obj.onreadystatechange = function() {            that.result = that.obj.responseText;            if(that.obj.readyState == 4 && that.obj.status == 200)            {                callback();            }        } 

is that what you mean?

Link to comment
Share on other sites

this is how I have it
 that.obj.onreadystatechange = function() {			that.result = that.obj.responseText;			if(that.obj.readyState == 4 && that.obj.status == 200)			{				callback();			}		} 

is that what you mean?

That's right.It's not really a problem, but it might be better if you put the other line of code inside the condition as well. There's no point updating that.result on every state change.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...