Jump to content

Function within a Function not working


Edward

Recommended Posts

Hi,

 

Before I go into too much detail unnecessarily, can anyone tell me why this doesn't work? The first code doesn't, but the second code does, however I would prefer to use the first one if possible. ONLY THE LAST LINE IS DIFFERENT.

 

Doesn't work:

function chat_event_add() {
# Disable the View function
clearInterval(interval_chat_event_view);
# Do the AJAX
xmlHttp_chat_event_add=GetXmlHttpObject_chat_event_add();
if (xmlHttp_chat_event_add==null) {
	alert ("Your browser does not support AJAX!");
	return;
} 
var url_chat_event_add="chat_event_add_sql.php"; 
url_chat_event_add=url_chat_event_add+"?form_chat_from=" + document.getElementById("form_chat_from").value;
url_chat_event_add=url_chat_event_add+"&form_chat_alias=" + document.getElementById("form_chat_alias").value;
url_chat_event_add=url_chat_event_add+"&form_chat_message=" + document.getElementById("form_chat_message").value;
url_chat_event_add=url_chat_event_add+"&rand="+Math.random();
xmlHttp_chat_event_add.onreadystatechange=stateChanged_chat_event_add;
xmlHttp_chat_event_add.open("GET",url_chat_event_add,true);
xmlHttp_chat_event_add.send(null);
document.getElementById("form_chat_message").value = '';
# Re-start the View function
chat_event_view();
}

 

Does work:

function chat_event_add() {
# Disable the View function
clearInterval(interval_chat_event_view);
# Do the AJAX
xmlHttp_chat_event_add=GetXmlHttpObject_chat_event_add();
if (xmlHttp_chat_event_add==null) {
	alert ("Your browser does not support AJAX!");
	return;
} 
var url_chat_event_add="chat_event_add_sql.php"; 
url_chat_event_add=url_chat_event_add+"?form_chat_from=" + document.getElementById("form_chat_from").value;
url_chat_event_add=url_chat_event_add+"&form_chat_alias=" + document.getElementById("form_chat_alias").value;
url_chat_event_add=url_chat_event_add+"&form_chat_message=" + document.getElementById("form_chat_message").value;
url_chat_event_add=url_chat_event_add+"&rand="+Math.random();
xmlHttp_chat_event_add.onreadystatechange=stateChanged_chat_event_add;
xmlHttp_chat_event_add.open("GET",url_chat_event_add,true);
xmlHttp_chat_event_add.send(null);
document.getElementById("form_chat_message").value = '';
# Re-start the View function
interval_chat_event_view = setInterval('chat_event_view()', 5*1000); // milliseconds between requests
}

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/153404-function-within-a-function-not-working/
Share on other sites

I fixed it by re-starting the View function in another part of the AJAX code, like so:

 

function stateChanged_chat_event_add() {

if (xmlHttp_chat_event_add.readyState==4) {

document.getElementById("thread").innerHTML=xmlHttp_chat_event_add.responseText;

$(function() {

$("#thread-container").scrollTo(10000000, 500);

});

 

// Following addition of message, refresh View

chat_event_view();

 

}

}

 

cleaned up your code abit ;)

function chat_event_add() {

   clearInterval(interval_chat_event_view);
   
   xmlHttp_chat_event_add=GetXmlHttpObject_chat_event_add();
   if (xmlHttp_chat_event_add===null) {
      alert ("Your browser does not support AJAX!");
      return;
   }
   var url_chat_event_add="chat_event_add_sql.php";
   url_chat_event_add=url_chat_event_add+"?form_chat_from=" + document.getElementById("form_chat_from").value;
   url_chat_event_add=url_chat_event_add+"&form_chat_alias=" + document.getElementById("form_chat_alias").value;
   url_chat_event_add=url_chat_event_add+"&form_chat_message=" + document.getElementById("form_chat_message").value;
   url_chat_event_add=url_chat_event_add+"&rand="+Math.random();
   xmlHttp_chat_event_add.onreadystatechange=stateChanged_chat_event_add;
   xmlHttp_chat_event_add.open("GET",url_chat_event_add,true);
   xmlHttp_chat_event_add.send(null);
   document.getElementById("form_chat_message").value = '';

   chat_event_view();
}

Archived

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

×
×
  • 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.