Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-12-2012, 02:09 AM   PM User | #1
Andrew Cay
New to the CF scene

 
Join Date: Dec 2012
Location: Arizona
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Andrew Cay is an unknown quantity at this point
Question Program slows over time, trying to make timer

I believe it is my usage of the Date() object. I am creating a new one every update in my system code ( it updates every 60 times a second ). I do this because I want a millisecond accurate clock useful for timers, animations, etc.


So I think it's because I use too much memory? Though I thought javascript has a kind of "trash" system that throws away unused "garbage". I've tried deleting variables of the Date but I get the same results of a slow down.

This is what's executed ever update:
Code:
function system()
{
	// time reference
		var Current = new Date();
	// system loop here
		step( Current ); // the local time variable gets passed to step
		draw();
}
Any help greatly appreciated!
Andrew Cay is offline   Reply With Quote
Old 12-12-2012, 02:34 AM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
I presume your code performs screen updates, and I suspect that they can't keep up with the frequency of the function calls. You haven't shown enough code to be certain.
Logic Ali is offline   Reply With Quote
Old 12-12-2012, 02:55 AM   PM User | #3
Andrew Cay
New to the CF scene

 
Join Date: Dec 2012
Location: Arizona
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Andrew Cay is an unknown quantity at this point
Quote:
Originally Posted by Logic Ali View Post
I presume your code performs screen updates, and I suspect that they can't keep up with the frequency of the function calls. You haven't shown enough code to be certain.
Problem: The fps is dramatically slowed.

The updates are run through a setInterval of function system 60 times a second.


This is unmodified version of the system
Code:
// init function
	function init()
	{
		loadImages();
		obj_player = new Player();
		map_init();
		background_color = "gray";
		if(typeof game_loop != "undefined")
			 clearInterval(game_loop);
		game_loop = setInterval( system, 1000/120);
	}
	// commence starting
		init();

       // system parts - unchanged, draw is not shown though (unnecessary)
                function step( Current )
                {
                    obj_player.step( Current );
                }

                function system()
                {
			// time reference
				var Current = new Date();
			// system loop here
				step( Current );
				draw();
			delete Current;
                }
this is a modified version of the Player object.
Code:
function Player()
{
this.face = 0;
this.time = 0;
this.gravity = 0;

this.step = function( Current )
{
	if ( this.time + 1000 < Current.getTime() )
	{
		bugs.innerHTML = Current.getTime();
		this.time = Current.getTime();
		if ( Math.random() * 2 > 1 && this.gravity == 0)
			this.gravity = -15;
		switch( Math.round( Math.random() * 2) )
		{
			case 0:
				this.face = 0;
			break;
			case 1:
				this.face = -1;
			break;
			case 2:
				this.face = 1;
			break;
		}
	}
}
}
I don't have user input in it yet, I know how to add that, I'm just messing with very basic AI .
So that's it. Thanks for the reply, Logically.

Last edited by Andrew Cay; 12-12-2012 at 02:57 AM.. Reason: Easier Reading
Andrew Cay is offline   Reply With Quote
Old 12-12-2012, 11:14 AM   PM User | #4
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by Andrew Cay View Post
Problem: The fps is dramatically slowed.

The updates are run through a setInterval of function system 60 times a second.
The delete statement has no effect on local variables.

You are calling system() every 8ms, which is close to the maximum that a browser will allow, and far higher than the frequency that you can expect screen updates to occur.

Is it necessary to call draw() if .face is not updated?
Logic Ali is offline   Reply With Quote
Old 12-12-2012, 11:39 AM   PM User | #5
OlgaRainbow
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
OlgaRainbow is an unknown quantity at this point
Thank you! I found this info useful for me.
OlgaRainbow is offline   Reply With Quote
Old 12-12-2012, 01:33 PM   PM User | #6
Andrew Cay
New to the CF scene

 
Join Date: Dec 2012
Location: Arizona
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Andrew Cay is an unknown quantity at this point
Quote:
Originally Posted by Logic Ali View Post
The delete statement has no effect on local variables.

You are calling system() every 8ms, which is close to the maximum that a browser will allow, and far higher than the frequency that you can expect screen updates to occur.

Is it necessary to call draw() if .face is not updated?
By the way, the code showing 1000/120 for the milliseconds to update is a test. It was showing me that the program is fast at first but slows not much later in the code.

Also, yes this is just a very basic program. The person moves around, so if I do not update according face not changing, the person will stay frozen in the spot till otherwise.

Thank's for your help still!
Andrew Cay is offline   Reply With Quote
Old 12-12-2012, 03:04 PM   PM User | #7
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by Andrew Cay View Post

Also, yes this is just a very basic program. The person moves around, so if I do not update according face not changing, the person will stay frozen in the spot till otherwise.
In that case you should only call draw() when that happens, then I'm sure you won't have a problem.
Logic Ali is offline   Reply With Quote
Old 12-13-2012, 01:26 AM   PM User | #8
Andrew Cay
New to the CF scene

 
Join Date: Dec 2012
Location: Arizona
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Andrew Cay is an unknown quantity at this point
Quote:
Originally Posted by Logic Ali View Post
In that case you should only call draw() when that happens, then I'm sure you won't have a problem.
I think what I said, could have been taken two ways haha, sorry. I meant that if I don't call draw, my character will look like hes staying still when he should be moving. Also, draw needs to be called in order to update the view of the game on the screen. Is this the reason it's slowing down? Are there better ways of making loops for a game?
Andrew Cay is offline   Reply With Quote
Old 12-13-2012, 01:55 AM   PM User | #9
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by Andrew Cay View Post
I think what I said, could have been taken two ways haha, sorry. I meant that if I don't call draw, my character will look like hes staying still when he should be moving. Also, draw needs to be called in order to update the view of the game on the screen. Is this the reason it's slowing down?
That is what I understood. Remove the call to draw from its shown location and insert it immediately after the switch statement in step.
Logic Ali is offline   Reply With Quote
Old 12-13-2012, 08:51 AM   PM User | #10
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Make certain you don't have multiple setIntervals active at the same time. If you start a second before clearing the first you will soon have as many processes running as the computer can handle.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:09 PM.


Advertisement
Log in to turn off these ads.