本文介绍了requestAnimationFrame范围更改为窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列看起来像这样的对象:

I have a chain of objects that looks like this:

Game.world.update()

我想使用requestAnimationFrame来确定此函数的帧速率。

I would like to use requestAnimationFrame to determine the framerate of this function.

但是当我按照这样实现它时:

However when I implement it like this:

World.prototype.update = function()
{
    requestAnimationFrame(this.update);
}

范围从世界对象变为窗口对象。 如何在调用requestAnimationFrame()时保持我想要的范围?我知道它与匿名函数等有关,但我无法理解它。

The scope changes from the world object to the window object. How do I maintain the scope I want while calling requestAnimationFrame()? I know it has something to do with anonymous functions and such, but I can't get my head around it.

推荐答案

通常的做法,无处不在:

Usual approach, works everywhere:

World.prototype.update = function()
{
    var self = this;
    requestAnimationFrame(function(){self.update()});
}

或者ES5 ():

World.prototype.update = function()
{
    requestAnimationFrame(this.update.bind(this)});
}

这篇关于requestAnimationFrame范围更改为窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 22:03