When the HTML body loads, I have two separate AJAX functions that need to be called. These functions should operate at different time intervals for different types of chat in order to distribute the server workload effectively. (No JQuery, please)
function executePOST(data1, data2) {
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
//Appending xml data to html in a loop
}
var date = new Date();
var datetime = date.getTime();
xmlhttp.open("POST", "page1.php", true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
var parameters = "data1="+data1+"&data2="+data2;
xmlhttp.send(parameters);
}
function executeGET(data1, data2) {
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
//Appending xml data to html in a loop
}
var date = new Date();
var datetime = date.getTime();
xmlhttp.open("GET", "page2.php?datetime="+datetime, true);
xmlhttp.send();
}
Each AJAX function works fine individually, but issues arise when trying to nest or schedule them at specific intervals.
function nestedExecution(data1, data2) {
executePOST(data1, data2);
executeGET(data1, data2);
}
OR
function scheduledExecution(data1, data2) {
setTimeout( function(){
executePOST(data1, data2);
}, 10000);
//Run every 10 seconds
setTimeout( function(){
executeGET(data1, data2);
}, 60000);
//Run every 60 seconds
}
Only the second AJAX call within the nesting executes, regardless of their order, and an error is returned for the first function.
TypeError: The return value of the AJAX "XML" is undefined.
This seems inaccurate as they work perfectly on their own.
The only workaround was placing them inside timed functions, but this prevented the first function from running every 10 seconds.
function mixedExecution(data1, data2) {
setTimeout( function(){
executePOST(data1, data2);
setTimeout( function(){
executeGET(data1, data2);
}, 60000);
}, 10000);
}