我试图通过每10毫秒轮询一次从芯片读取值。该函数每读取200次左右就会退出并显示错误。当它引发错误时,我想重新启动该函数,直到它正常退出(到达循环末尾)为止。
const MAX_ITERATIONS = 14
const DELAY = 20
let iteration = 0
let doSomething = calibrateChip(() => {
let int = setInterval(() => {
getValue((val) => { // This function throws errors
if (val > 10) iteration = 0 // Reset the counter
else iteration++
})
if (iteration >= MAX_ITERATIONS) {
clearInterval(int)
// exit the loop
}
}, DELAY)
})
try {
doSomething
}
catch (error) {
doSomething // keep trying until iteration == MAX_ITERATIONS
}
这可以通过try / catch块来完成吗?
如果是,该函数如何继续尝试直到iteration == MAX_ITERATIONS
?