2 回答
TA贡献1828条经验 获得超3个赞
您可以使用setTimeout并等待请求完成来连续调用该函数
var timeoutPtr;
/**
* Execute a request, can take DELTA miliseconds
*/
function executeRequest() {
return new Promise((resolve) => {
var request = new XMLHttpRequest();
// ...
request.onload = function() {
// ...
resolve();
};
request.send();
});
}
/**
* Execute the request, when it's over, wait 2s and execute it again
* and again, and again...
*/
function executeRequestInARow() {
// In case you call executeRequestInRow multiple time
clearTimeout(timeoutPtr);
executeRequest()
.then(() => {
timeoutPtr = setTimeout(function() {
executeRequestInARow();
}, 2000);
});
}
executeRequestInARow();
添加回答
举报