Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Location
    Arizona
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Location
    Arizona
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    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?

  • #5
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you! I found this info useful for me.

  • #6
    New to the CF scene
    Join Date
    Dec 2012
    Location
    Arizona
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!

  • #7
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    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.

  • #8
    New to the CF scene
    Join Date
    Dec 2012
    Location
    Arizona
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #9
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    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.

  • #10
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,619
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •