我发送一个ajax GET请求与javascript setInterval每5秒。我想停止ajax GET调用时收到的响应状态为'COMPLETED'。因为我把清晰的间隔称为它的条件。但是它并没有停止setInterval
var intervalId = window.setInterval(function() {
console.log('Trigger!!');
$.ajax({
type: "GET",
enctype: 'application/json',
url: url,
processData: false,
contentType: false,
crossDomain: true,
cache: false,
timeout: 600000,
success: function(dataNew) {
console.log('dataaa get' + JSON.stringify(dataNew));
if (dataNew.jobStatus === 'COMPLETED') {
$('#txtMessage').text('Upload complete.');
clearInterval(intervalId);
}
},
error: function(e) {
$('#btnSubmit').prop("disabled", false);
$('#txtMessage').text('Error Occured');
clearInterval(intervalId);
},
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
},
complete: function() {
}
});
}, 5000);
使用如下ajax调用:
var interval = setInterval(callAjaxFunc, 5000);
function callAjaxFunc() {
console.log('Trigger!!');
$.ajax({
type: "GET",
enctype: 'application/json',
url: url,
processData: false,
contentType: false,
crossDomain: true,
cache: false,
timeout: 600000,
success: function(dataNew) {
console.log('dataaa get' + JSON.stringify(dataNew));
if (dataNew.jobStatus === 'COMPLETED') {
$('#txtMessage').text('Upload complete.');
clearInterval(interval);
}
},
error: function(e) {
$('#btnSubmit').prop("disabled", false);
$('#txtMessage').text('Error Occured');
clearInterval(interval);
},
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
},
complete: function() {
clearInterval(interval);
}
});
}