在类中使用 requestAnimationFrame [英] Use requestAnimationFrame in a class
本文介绍了在类中使用 requestAnimationFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在了解如何在类中使用 requestAnimationFrame
时遇到问题.
I'm having problem finding out how I can use requestAnimationFrame
in a class.
此代码运行良好:
window.onload = function () {
var width = 20;
function animation() {
width++;
var element = document.getElementById("box");
element.style.width = width + "px";
requestAnimationFrame(animation);
}
requestAnimationFrame(animation);
};
但是当我尝试把它放到一个类中时,我没有得到任何结果.
But when I try putting it into a class, I don't get any result.
class Animation{
width: number = 20;
constructor() {
requestAnimationFrame(this.loop);
}
loop() {
this.width++;
var element = document.getElementById("box");
element.style.width = this.width + "px";
requestAnimationFrame(this.loop);
}
}
window.onload = function () {
var animation = new Animation();
};
有人能告诉我这里有什么问题吗?
Could someone tell me what's wrong here?
推荐答案
requestAnimationFrame(this.loop);
如果你向其他人传递一个他们正在执行的函数调用使用箭头即 requestAnimationFrame(()=>this.loop());
或 loop = () =>{
requestAnimationFrame(this.loop);
If you are passing someone else a function that they are going to call use an arrow i.e requestAnimationFrame(()=>this.loop());
or loop = () => {
更多:https://www.youtube.com/watch?v=tvocUcbCupA
这篇关于在类中使用 requestAnimationFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文