我有一个setInterval,每100毫秒执行一次函数
问题是setInterval不会等到函数完全执行后才开始
这里是代码:
function Main(){
function AnyFunction(){
setTimeout(function(){
console.log('Anything');
},2000)
}
setInterval(AnyFunction,100) ;
}
Main();
在函数执行中确切发生了什么,我如何使setInterval等到函数执行?
const anyFunction = () => {
return new Promise((resolve, reject) => setTimeout(resolve, 2000));
}
const cycleAsync = (fn, time) => {
const timerId = setTimeout(() => {
fn()
.then(() => {
clearTimeout(timerId);
cycle(fn, time);
});
}, time);
}
cycleAsync(anyFunction, 200);