Edward Posted April 9, 2009 Share Posted April 9, 2009 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 More sharing options...
Edward Posted April 9, 2009 Author Share Posted April 9, 2009 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 https://forums.phpfreaks.com/topic/153404-function-within-a-function-not-working/#findComment-805952 Share on other sites More sharing options...
Edward Posted April 9, 2009 Author Share Posted April 9, 2009 Actually I didn't fix it, so could anyone help? Sorry! Link to comment https://forums.phpfreaks.com/topic/153404-function-within-a-function-not-working/#findComment-805967 Share on other sites More sharing options...
darkfreaks Posted April 10, 2009 Share Posted April 10, 2009 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 https://forums.phpfreaks.com/topic/153404-function-within-a-function-not-working/#findComment-805983 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.