如何解决如何错误处理 requestAnimationFrame 函数
正在寻找一种方法来检查 animate() 函数中的任何错误。如果 animate() 函数中发生任何错误,我的浏览器就会崩溃并且我的计算机会发热。
我试图将代码放在 try/catch 处理程序中,但这不起作用
animate(){
this.renderer.render(this.scene,this.camera);
try {
// functions that update scene
} catch (error) {
gsap.ticker.remove(() => this.animate());
console.error(error);
}
}
关于如何正确处理“requestAnimationFrame”/gsap.tick 循环的任何建议?
解决方法
您的 .remove()
函数没有删除该函数的原因是,当您使用箭头函数时,您每次都在创建一个新函数。因此,它会尝试删除一个它刚刚创建的新函数,而不是删除 .add()
中的函数。
我认为你想做这样的事情? Demo
animate() {
const myThis = window.tunnel;
try {
// Working version
myThis.renderer.render(myThis.scene,myThis.camera);
// Error version (to show it works)
// this.renderer.render(this.scene,this.camera);
} catch (error) {
gsap.ticker.remove(myThis.animate);
console.error(error);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。