当页面加载时,我每隔30秒就发出一个 AJAX 请求来加载一个json文件:
jQuery(document).ready(function () {
loadBuildInterval = setInterval(loadBuildData, intervalTime); //Start interval when document is loaded
});
function loadBuildData() {
jQuery.ajax({
url: fileUrl + '?v' + (Math.round(new Date()/1000)), //Add unique version to json file every time it is requested (cache busting).
type: 'POST',
data: data,
dataType: 'json',
success: function (result) {
clearInterval(loadBuildInterval); //Reload interval with new duration
loadBuildInterval = setInterval(loadBuildData, intervalTime);
},
error: function () {
clearInterval(loadBuildInterval);
console.log('Reload Changes - Aborting - ajax error.');
}
});
}
当它到达error函数时,它开始以浏览器所能达到的最快速度发出 AJAX 请求,而不是清除间隔。
1条答案
按热度按时间roejwanj1#
我建议使用
setTimeout
而不是setInterval
,试试这个: