window.GetAnimationFrame() 不是在调用回调函数
window.GetAnimationFrame() Is not calling callback function
我在 TypeScript 中这样做,但如果有帮助,我也会 post 编译的 Javascript
。以下是我的 loop()
函数:
TypeScript
private loop() {
if(this._running) {
// input, updating, and rendering
// ...
window.requestAnimationFrame(() => this.loop);
}
}
JavaScript
PotatoEngine.prototype.loop = function () {
var _this = this;
if (this._running) {
// input, updating, and rendering
// ...
window.requestAnimationFrame(function () { return _this.loop; });
}
};
从以下 init()
函数调用此代码:
TypeScript
private init() {
this._lastTime = Date.now();
this._running = true;
window.requestAnimationFrame(() => this.loop);
}
JavaScript
PotatoEngine.prototype.init = function () {
var _this = this;
this._lastTime = Date.now();
this._running = true;
window.requestAnimationFrame(function () { return _this.loop; });
};
当我调试并到达 requestAnimationFrame()
时,它永远不会进入 loop()
函数。它刚刚退出 init()
。为什么?
变化:
window.requestAnimationFrame(() => this.loop);
收件人:
window.requestAnimationFrame(this.loop.bind(this));
这实际上会调用this.loop
。由于绑定,您还可以保留上下文。
我在 TypeScript 中这样做,但如果有帮助,我也会 post 编译的 Javascript
。以下是我的 loop()
函数:
TypeScript
private loop() {
if(this._running) {
// input, updating, and rendering
// ...
window.requestAnimationFrame(() => this.loop);
}
}
JavaScript
PotatoEngine.prototype.loop = function () {
var _this = this;
if (this._running) {
// input, updating, and rendering
// ...
window.requestAnimationFrame(function () { return _this.loop; });
}
};
从以下 init()
函数调用此代码:
TypeScript
private init() {
this._lastTime = Date.now();
this._running = true;
window.requestAnimationFrame(() => this.loop);
}
JavaScript
PotatoEngine.prototype.init = function () {
var _this = this;
this._lastTime = Date.now();
this._running = true;
window.requestAnimationFrame(function () { return _this.loop; });
};
当我调试并到达 requestAnimationFrame()
时,它永远不会进入 loop()
函数。它刚刚退出 init()
。为什么?
变化:
window.requestAnimationFrame(() => this.loop);
收件人:
window.requestAnimationFrame(this.loop.bind(this));
这实际上会调用this.loop
。由于绑定,您还可以保留上下文。