requestAnimationFrame 的 RangeError
我收到以下错误:
RangeError: Maximum call stack size exceeded.
在这一行:
requestAnimFrame(Game.loop());
在这段代码中:
var Game = {
canvas: null,
context: null,
targetFps: 60,
init: function() {
canvas = document.getElementById('main-canvas');
context = canvas.getContext('2d');
// Resize canvas to match content
var content = document.getElementById('primary-content');
canvas.width = content.offsetWidth;
canvas.height = content.offsetHeight;
window.requestAnimFrame = (function() {
return window.requestAnimationFrame || // Chromium
window.webkitRequestAnimationFrame || // WebKit
window.mozRequestAnimationFrame || // Mozilla
window.oRequestAnimationFrame || // Opera
window.msRequestAnimationFrame || // IE
null;
})();
this.loop();
},
loop: function() {
requestAnimFrame(Game.loop());
},
update: function() {
// ...
},
draw: function() {
// Clear background with black
context.fillStyle = '#000';
context.fillRect(0, 0, canvas.width, canvas.height);
}
};
Game.init();
<div id="primary-content">
<canvas id="main-canvas" width="400" height="400"></canvas>
</div>
这可能是我忽略的明显事情,但任何帮助将不胜感激。谢谢!
I'm getting the following error:
RangeError: Maximum call stack size exceeded.
on this row:
requestAnimFrame(Game.loop());
in this piece code:
var Game = {
canvas: null,
context: null,
targetFps: 60,
init: function() {
canvas = document.getElementById('main-canvas');
context = canvas.getContext('2d');
// Resize canvas to match content
var content = document.getElementById('primary-content');
canvas.width = content.offsetWidth;
canvas.height = content.offsetHeight;
window.requestAnimFrame = (function() {
return window.requestAnimationFrame || // Chromium
window.webkitRequestAnimationFrame || // WebKit
window.mozRequestAnimationFrame || // Mozilla
window.oRequestAnimationFrame || // Opera
window.msRequestAnimationFrame || // IE
null;
})();
this.loop();
},
loop: function() {
requestAnimFrame(Game.loop());
},
update: function() {
// ...
},
draw: function() {
// Clear background with black
context.fillStyle = '#000';
context.fillRect(0, 0, canvas.width, canvas.height);
}
};
Game.init();
<div id="primary-content">
<canvas id="main-canvas" width="400" height="400"></canvas>
</div>
It's probably something obvious I'm overlooking, but any help would be appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您立即调用该函数而不是传递它:
只会一遍又一遍地执行
Game.loop()
。您想要的是:
这会将函数作为参数传递给
requestAnimFrame
,而不是调用Game.loop()
并传递结果 (undefined)。
You are calling the function right away instead of passing it:
Will just keep doing
Game.loop()
over and over again.What you want is:
this will pass the function as an argument to
requestAnimFrame
, instead of callingGame.loop()
and passing the result (undefined
).