最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - RangeError with requestAnimationFrame - Stack Overflow

programmeradmin3浏览0评论

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>

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!

Share Improve this question edited Sep 16, 2019 at 6:42 jdlm asked Jan 7, 2012 at 18:16 jdlmjdlm 6,6545 gold badges33 silver badges50 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 17

You are calling the function right away instead of passing it:

loop: function() {
        requestAnimFrame(Game.loop());
    }

Will just keep doing Game.loop() over and over again.

What you want is:

loop: function() {
        requestAnimFrame(Game.loop);
    }

this will pass the function as an argument to requestAnimFrame, instead of calling Game.loop() and passing the result (undefined).

发布评论

评论列表(0)

  1. 暂无评论