I have created this script to monitor the progress of an import script. The script is designed to execute a function that makes an http request every X seconds.
function checkImportProgress() {
//if(import_status != 'finalizat') {
alert("Checking Import Progress");
setTimeout(function() { return updateImportProgress(); }, 2000);
setTimeout(function() { return updateImportProgress(); }, 4000);
setTimeout(function() { return updateImportProgress(); }, 6000);
setTimeout(function() { return updateImportProgress(); }, 8000);
//setTimeout(function() { checkImportProgress(); }, 400);
//}
//else {
//}
}
This script was used for testing purposes with comments included to indicate the intended functionality. Various combinations of setTimeout calls were tried, including using quotes, not using quotes, and using or not using anonymous functions.
var xmlhttp_import_progress;
function updateImportProgress() {
xmlhttp_import_progress=GetXMLHttpObject();
if (xmlhttp_import_progress==null) {
alert ("Browser does not support HTTP Request (xmlhttp_import_progress)");
return;
}
var url="crm/windows/import_progress.php";
url=url+"?sid="+Math.random();
xmlhttp_import_progress.onreadystatechange=function() {
if (xmlhttp_import_progress.readyState == 4) {
progress_resp = xmlhttp_import_progress.responseText;
progress = progress_resp.split('_');
import_num_completed = progress[0];
import_total = progress[1];
import_status = progress[2];
message = 'Import Progress: ' + import_num_completed + ' / ' + import_total;
//document.getElementById("import_message_body").innerHTML = message;
alert(progress_resp);
}
};
xmlhttp_import_progress.open("POST",url,true);
xmlhttp_import_progress.send(null);
}
This section contains the main logic of the checkImportProgress function.
When running the script, I receive the "Checking Import Progress" alert at the start of the import process. However, the alert displaying the progress details only shows up after the import process has completed (even though it continues to maintain the 2-second interval as intended by the setTimeout calls).
The PHP script in the AJAX request simply fetches session variables set by the import script and sends them back to JavaScript for display (such as number of imports completed out of total, number failed, etc).
Any suggestions on why this behavior occurs?