Jump to content
  • Advertisement
Sign in to follow this  
jdub

Newbie Javascript question

This topic is 1955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I have this javascript code:

 

var sim = new function()
{
...
...
...
this.Tick = function () 
	{
		var currTime = Date.now();
		var elapsedMS = currTime - this.lastUpdateTime;

		this.Update(elapsedMS);
		this.Render();

		this.lastUpdateTime = currTime;
	};

this.Run = function()
	{
		this.tickIntervalId = setInterval(this.Tick, 1000/60); //Update at 60 fps
	};

Getting the following error: Uncaught ReferenceError: Update is not defined.

Anybody know what's causing this?

 

Share this post


Link to post
Share on other sites
Advertisement

The problem is right there in the error message: You are calling 

this.Update(elapsedMS);

 

in your this.Tick function, but you have not defined an Update function anywhere.

Share this post


Link to post
Share on other sites

Oops.  I realized that I only posted part of the code I have.  Here is more relevant code:

 

//create singleton class
var sim = new function()
{
...
this.Run = function()
	{
		this.tickIntervalId = setInterval(this.Tick, 1000/60); //Update at 60 fps
	};
...

this.Update = function (elapsedMS) {
		
	  for (var i = 0; i < nodeArr.length; i++) {
		nodeArr.Update();
	  }
	};
...
this.Tick = function () 
	{
		var currTime = Date.now();
		var elapsedMS = currTime - this.lastUpdateTime;

		this.Update(elapsedMS);
		this.Render();

		this.lastUpdateTime = currTime;
	};
};

sim.Run();

Share this post


Link to post
Share on other sites

In JavaScript functions don't really have a "context" associated statically with them, it is evaluated dynamically .. setTimeout and setInterval basically "reset" the context and your call to "this.Tick" doesn't know what "this" is.

 

Change 

        this.tickIntervalId = setInterval(this.Tick, 1000/60); //Update at 60 fps
 
To
        this.tickIntervalId = setInterval(function() { this.Tick() }, 1000/60); //Update at 60 fps

To not lose the context
 
Edit: missing parenthesis on "To" -line's call to Tick()
 
Another edit:
Actually, not 100% sure of the "this.Tick()" inside that function either, the only sure-fire way to not lose "this" is to define something like var me = this; on the line above and call me.Tick() instead.
 
I.e.:
 
var me = this;
this.tickIntervalId = setInterval(function() { me.Tick() }, 1000/ 60);
Edited by lietu

Share this post


Link to post
Share on other sites

If you are running on chrome, you can simply use the bind function, 

this.tickIntervalId = setInterval(this.Tick.bind(this), 1000/60); //Update at 60 fps

 

or if you are using the popular underscore utility

 

this.tickIntervalId = setInterval(_.bind(this.Tick,this), 1000/60); //Update at 60 fps

 

 

The problem is that your Tick function gets called in a global context. Which means that, the 'this' pointer is the global 'this' which is not what you need. Hence you need to bind the 'this' context so that when the Tick function gets called from a global context, the original 'this' pointer is still saved.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!