如何每 x 秒制作一次动画 运行

How to make the animation run every x seconds

我想要一个动画师,如果我有这样的

index = 0;
function animate() {
    index++;
    requestAnimationFrame(animate);
}

但是我怎样才能让 index++ 每隔 x 秒一次? 所以如果 x 是 5 例如索引 += 1 每 5 秒/动画每 5 秒循环一次。

可以在js中查找setInterval()函数
首先是 link https://www.w3schools.com/jsref/met_win_setinterval.asp

类似的内容应该对您有所帮助

let start = Date.now();

function foo() {
    if(Date.now() - start > 5000){
    console.log('hit')
   start = Date.now()
}
 requestAnimationFrame(foo);
}

foo();

你可以这样使用setInterval()

index = 0;
function animate() {
    index++;
    console.log(index)
    //requestAnimationFrame(animate);
}

setInterval(function(){  
  animate();
}, 5000);