我有一个 js 文件,我正在使用 requestAnimationFrame()。我知道参数应该是回调函数。我认为我的参数是回调函数,但错误来自 js 控制台:
TypeError: Argument 1 of Window.requestAnimationFrame is not an object.
文件.js:
function screen(){
console.log("it works!")
}
function fun(word){
if (word === 'tree'){
screen()
}
window.requestAnimationFrame(fun("tree"));
一切正常。我在我的大型项目中使用它并且一切正常,但我不知道为什么我在 js 控制台中出现错误
最佳答案
您将 fun
函数的执行结果传递给 requestAnimationFrame
,这就是您收到错误的原因(因为它返回未定义)。
正确的做法:
function screen(){
console.log("it works!")
}
function fun(word){
if (word === 'tree'){
screen()
}
}
window.requestAnimationFrame(fun.bind(window, "tree"));
有关bind
的更多信息,您可以在documentation 中找到.
关于javascript - 作为 window.requestAnimationFrame() 参数的回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48086337/