I have a weird issue with an jQuery AJAX jQuery request. When we generate reports on our site, it can take a while, so we have a status bar that we update with polling. However, what I'm seeing is that these requests seem to be timing out and re-sending - a very weird behavior.
Here's the workflow: -User puts in their email address and clicks "Get Report" -Button triggers the AJAX request (see below). -After making the AJAX request, we send a second, simpler request, to poll a checkStatus function. This function returns a JSON string to a callback that updates the status bar on the screen. -In the original AJAX request's "success" property, we have set a flag to stop the polling, so when the report completes, the polling stops (and the last status update reflects that it's finished).
This works perfectly, except for one small problem. I'm finding that when we have big reports, it seems to send a second getReport request without the user actually doing anything. I can tell this because our status will get to about 700 out of 1000 rows, and then jump back to 15 out of 1000 then 710 out of 1000 then 25 out of 1000, indicating that two reports are being generated simultaneously and updating the status flag with different statuses.
All I can figure is that the original getReport AJAX request is being re-sent for some reason. Is there some timeout or something that I'm not aware of?
Here's the code:
var update = false;
//// Function triggered by user when requesting report
function get_report () {
if ( !$("#reportEmail").val() ) {
alert ( "Please enter a valid email address to send the report to." );
return;
}
var dataStr = "email="+$("#reportEmail").val();
$("#report").html ( "<b>Your report is being generated and will be emailed to you momentarily.</b>" );
$.ajax({
type: 'POST',
data: dataStr,
url: "/reports/roster/15",
success: function() { update = false; }
});
$("#completed").css ( 'width',"0" );
update = true;
setTimeout ( "checkStatus('reg_roster', updateStatusBar);", 10 );
}
//// Call back from the checkStatus function
function updateStatusBar ( data ) {
response = jQuery.parseJSON(data);
if ( response ) {
$("#statusBar").show();
$("#completed").css ( 'width', response.completed+"%" );
$("#msg").html ( response.message );
if ( update == true )
setTimeout ( "checkStatus('reg_roster', updateStatusBar);", 1000 );
}
}
////Polling function to check status
function checkStatus(type, callback) {
$.ajax({
type: 'POST',
async: false,
data: "&type="+type,
url: "/status/check/",
success: callback
});
}