Aborting $.ajax causes uncleared setInterval in $.ajax & ongoing CPU usage
Hi,
I have submitted a ticket regarding an issue with aborting ajax
requests: http://dev.jquery.com/ticket/2935
I thought I'd post it here in case anyone has any additional feedback.
Regards,
Ian
------------------
Issue:
Aborting an ajax request results in $.ajax having an uncleared
setInterval which causes ongoing CPU usage (jQuery 1.2.5).
Test code:
var myajax = $.ajax(...);
myajax.abort();
Details:
When using abort() on a returned xhr object from $.ajax(), the xhr
will be successfully aborted, but the 'ival' setInterval for
onreadystatechange in $.ajax will never be cleared because the
onreadystatechange condition that contains the clearInterval will
never be met now that the xhr has been aborted.
// from onreadystatechange in $.ajax
if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout ==
"timeout") ) {
// this will never be reached after issueing myaxjax.abort()
// and the clearInterval will never happen
}
The existence of the uncleared interval causes ongoing CPU usage and
becomes magnified as more requests are aborted and their intervals
left uncleared.
Suggested fix:
readyState will be is set to 0 when abort() is called. Add a check for
readyState == 0 to onreadystatechange and clear the interval on this
condition.
var onreadystatechange = function(isTimeout){
if (xhr.readyState == 0)
{
// xhr has been aborted, clear the interval
if (ival) {
clearInterval(ival);
ival = null;
}
}
else if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout ==
"timeout") ) {
// current existing code, including clear poll interval
}