Dankey Dankey - 1 year ago 120
Node.js Question

Property is becoming NaN

I have the following code for a small game server.

var GameWorld = function () {

this.uptime = 0;

this.loop = function () {


var game = new GameWorld();
setInterval(game.loop, 500);

However then when I look at
, it returns NaN

Answer Source

This is happening because when game.loop is called with the given interval, this does not return the correct object (read Understanding JavaScript's "this")

To change this, you simply need to bind game when calling game.loop:

setInterval(game.loop.bind(game), 500);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download