我有一個運行的代碼塊,我想每 2 秒回圈一次,直到結果(val1、val2、val3)不再為空。我怎樣才能改變和包裝它來做到這一點?
// get the data
let theData = null;
try {
theData = await gRating.loadData(request_code)
} catch (ex) {
console.error(ex);
respondWithError(res, 'API error', 500);
return;
}
loadData 函式回傳一個陣列并將其存盤在 theData 中,如下所示:
return [this.val1, this.val2, this.val3]
uj5u.com熱心網友回復:
您可以使用setInterval并檢查是否theData不是空值,然后clearInterval
let theData = null;
let timeout = setInterval(async () => {
try {
theData = await gRating.loadData(request_code);
if (theData) {
clearInterval(timeout);
}
} catch (ex) {
console.error(ex);
respondWithError(res, "API error", 500);
}
}, 2000);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/512374.html
