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
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();

 

}

}

Link to comment
Share on other sites

 

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();
}

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.