每隔一段时间wait执行一个函数 执行一定次数times次
<script>
function repeat(fun, times, wait) {
return function (content) {
let count = 0;
let interval = setInterval(() => {
count = count + 1;
fun(content);
if (count === times) {
clearInterval(interval);
}
}, wait)
}
}
function repeatfunc(meg) {
console.log(meg);
}
let test = repeat(repeatfunc, 5, 2000);
test(123);
</script>
上面的例子每隔2s执行一次repeatfunc函数,执行五次后结束