时间间隔嵌套在循环内部

时间:2019-02-26 00:24:13

标签: javascript

我正在尝试在window.requestAnimationFrame循环中适应一个重复的时间间隔,但是我不确定如何格式化语法以使用诸如SetInterval()之类的方法,但这似乎行不通。 ..无论如何添加setInterval()我无法使它正常工作……有什么建议吗?

const renders = function(){
window.requestAnimationFrame(rendres)
setInterval(Dostuff,3000) //right here
}
window.requestAnimationFrame(renders)

1 个答案:

答案 0 :(得分:1)

const renders = function(){
window.requestAnimationFrame(rendres)
 //do stuff
}
// don't do this
// window.requestAnimationFrame(renders) 

// do this instead
let timer = setInterval(window.requestAnimationFrame(rendres),3000);

然后,如果要删除计时器,可以使用clearInterval(timer)