I am trying to put together a web interface for a process which downloads a number of XML files and then processes them. I am usimg JavaScript / jQuery / AJAX to initiate the process on the server, one file at a time, and to display progress information after each step.
My problem is that although everything seems to work, the progress messages only appear on the browser in one go after the entire process is completed, which is kinda pointless.
Here is my code - any suggestions will be welcome:
jQuery(document).ready(function($) {
$('#start_form').ajaxForm({
async: false,
beforeSubmit: function(formData, jqForm, options) {
document.getElementById('submit_button').disabled = true;
document.getElementById('progress-messages').innerHTML = '';
},
success: function(responseText, statusText, xhr, $form) {
if (responseText == '1') {
document.getElementById('progress-messages').innerHTML += '<h3>Retrieving XML Data from Amrod</h3>';
var files = ['products', 'categories', 'images', 'branding'];
var i = 0;
while (files[i]) {
$.ajax({
type: "POST",
url: ajaxurl,
async: false, // Hopefully wait for a response from the server?
data: {
action: 'update_products',
file: files[i]
},
success: function(response) {
document.getElementById('progress-messages').innerHTML += '<p>Retrieved ' + response + ' file</p>';
setTimeout(function() {
// Do nothing for a sec to see if the innerHTML will display - it doesn't.
}, 1000);
alert(); // This causes the innerHTML to display in the correct sequence, but is a pain - not a solution.
i++;
}
});
}
}
document.getElementById('progress-messages').innerHTML += '<h3>Processing XML Data</h3>';
// Do other stuff
document.getElementById('submit_button').disabled = false;
return false;
}
});
});