Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6239101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:20:08+00:00 2026-05-24T11:20:08+00:00

Is it possible to use setTimout() within a JavaScript object? Currently the animation method

  • 0

Is it possible to use setTimout() within a JavaScript object?

Currently the animation method call is running once, it seems that the setTimeout() isn’t doing its job. I have managed to get it working, but in a really hackish method of having a function outside of the class which uses the setTimeout. I’d like to make the animation loop a job for the AnimationManager class. If you can see any bad practice, or where i’m going wrong.. please give me a heads up!

JavaScript:

var AnimationManager = function(canvas)
{
    this.canvas = canvas;
    this.canvasWidth = canvas.width();
    this.canvasHeight = canvas.height();
    this.ctx = canvas.get(0).getContext('2d');
    this.running = true;

    this.start = function start(){
        this.running = true;
        this.animate();
    }

    /** Allow the animations to run */
    this.run = function run(){
        this.running = false;

    } 
    /** Stop the animations from running */    
    this.stop = function stop(){
        this.running = false;
    }

    this.animate = function animate()
    {
        if(this.running)
        {
            this.update();
            this.clear();
            this.draw();
        }
        setTimeout(this.animate, 40); //25 fps
    }

    /** Update all of the animations */
    this.update = function update()
    {
        for(var i in shapes)
        {
            shapes[i].moveRight();
        }
    }

    /** Clear the canvas */
    this.clear = function clear()
    {      
        this.ctx.clearRect(0,0, this.canvasWidth, this.canvasHeight);  
    }

    /** Draw all of the updated elements */
    this.draw = function draw()
    {       
        for(var i in shapes)
        {
            this.ctx.fillRect(shapes[i].x, shapes[i].y, shapes[i].w, shapes[i].h);
        }
    }
}

JavaScript within the index page, which demonstrates how i’d like the AnimationManager to work:

<script type="text/javascript">
    $(document).ready(function() {
        var canvas = $('#myCanvas');
        var am = new AnimationManager(canvas);
        am.start();

        //If true play the animation
        var startButton = $("#startAnimation");
        var stopButton = $("#stopAnimation");

        stopButton.hide();
        //Toggle between playing the animation / pausing the animation
        startButton.click(function() 
        {
            $(this).hide();
            stopButton.show();
            am.run();
        });

        stopButton.click(function() 
        {
            $(this).hide();
            startButton.show();
            am.stop();
        });  
    });
</script>  

Here’s the working code, thanks to T.J. Crowder for fix + interesting blog post: Double-take

Solution: Changes in code are marked with //#########

var shapes = new Array();
shapes.push(new Shape(0,0,50,50,10));
shapes.push(new Shape(0,100,100,50,10));
shapes.push(new Shape(0,200,100,100,10));

/**
 *  AnimationManager class
 *  animate() runs the animation cycle
 */
var AnimationManager = function(canvas)
{
    this.canvas = canvas;
    this.canvasWidth = canvas.width();
    this.canvasHeight = canvas.height();
    this.ctx = canvas.get(0).getContext('2d');
    this.running = true;
    var me = this; //#################################Added this in    

    this.start = function(){
        this.running = true;
        this.animate();
    }

    /** Allow the animations to run */
    this.run = function(){
        this.running = true;

    } 
    /** Stop the animations from running */    
    this.stop = function(){
        this.running = false;
    }

    this.animate = function()
    {
        if(this.running)
        {
            this.update();
            this.clear();
            this.draw();
        }
        //###################### Now using me.animate()
        setTimeout(function(){
            me.animate(); 
        }, 40); //25 fps
    } 

    /** Update all of the animations */
    this.update = function()
    {
        for(var i in shapes)
        {
            shapes[i].moveRight();
        }
    }

    /** Clear the canvas */
    this.clear = function()
    {      
        this.ctx.clearRect(0,0, this.canvasWidth, this.canvasHeight);  
    }

    /** Draw all of the updated elements */
    this.draw = function()
    {       
        for(var i in shapes)
        {
            this.ctx.fillRect(shapes[i].x, shapes[i].y, shapes[i].w, shapes[i].h);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:20:09+00:00Added an answer on May 24, 2026 at 11:20 am

    The problem with the code is that in JavaScript, this is set (in the normal case) by how a function is called, not where it’s defined. This is different than some other languages you might be used to such as Java or C#. So this line:

    setTimeout(this.animate, 40);
    

    …will indeed call your animate function, but with this set to the global object (window, on browsers). So all of those properties you’re accessing (this.running, etc.) will not be looking at your object, but rather looking for those properties on window, which is clearly not what you want.

    Instead, you can use a closure:

    var me = this;
    setTimeout(function() {
        me.animate();
    }, 40);
    

    That works because the anonymous function we’re giving to setTimeout is a closure over the context in which it’s defined, which includes the me variable we’re setting up before defining it. By calling animate from a property on the object (me.animate()), we’re telling JavaScript to set up this to be the object during the call.

    Some frameworks have methods to create this closure for you (jQuery has jQuery.proxy, Prototype has Function#bind), and ECMAScript 5 (about 18 months old) defines a new Function#bind feature for JavaScript that does it. But you can’t rely on it yet in browser-based implementations.

    More discussion and solutions here: You must remember this


    Possibly off-topic: In your code, you’re using a lot of named function expressions. E.g.:

    this.animate = function animate() { ... };
    

    Named function expressions don’t work correctly on IE prior to, I think, IE9. IE will actually create two completely separate functions (at two separate times). More here: Double-take


    Update and a bit off-topic, but since all of your functions are defined as closures within your AnimateManager constructor anyway, there’s no reason for anything you don’t want to be public to be public, and you can completely get rid of issues managing this.

    Here’s the “solution” code from your updated question, making use of the closures you’re already defining to avoid this entirely other than when defining the public functions. This also uses array literal notation for shapes and a normal for loop (not for..in) for looping through the array (read this for why: Myths and realities of for..in):

    var shapes = [
        new Shape(0,0,50,50,10)),
        new Shape(0,100,100,50,10)),
        new Shape(0,200,100,100,10))
    ];
    
    /**
     *  AnimationManager class
     *  animate() runs the animation cycle
     */
    var AnimationManager = function(canvas)
    {
        var canvasWidth = canvas.width(),
            canvasHeight = canvas.height(),
            ctx = canvas.get(0).getContext('2d'),
            running = true, // Really true? Not false?
            me = this;
    
        // Set up our public functions
        this.start = AnimationManager_start;
        this.run   = AnimationManager_run;
        this.stop  = AnimationManager_stop;
    
        /** Start the animations **/
        function AnimationManager_start(){
            running = true;
            animate();
        }
    
        /** Allow the animations to run */
        function AnimationManager_run(){
            running = true;
        } 
    
        /** Stop the animations from running */    
        function AnimationManager_stop(){
            running = false;
        }
    
        /** Internal implementation **/
        function animate()
        {
            if (running)
            {
                update();
                clear();
                draw();
            }
    
            setTimeout(animate, 40); //25fps
        } 
    
        /** Update all of the animations */
        function update()
        {
            var i;
    
            for (i = 0; i < shapes.length; ++i) // not for..in
            {
                shapes[i].moveRight();
            }
        }
    
        /** Clear the canvas */
        function clear()
        {      
            ctx.clearRect(0,0, canvasWidth, canvasHeight);  
        }
    
        /** Draw all of the updated elements */
            function draw()
        {       
            var i;
    
            for (i = 0; i < shapes.length; ++i) // not for..in
            {
                ctx.fillRect(shapes[i].x, shapes[i].y, shapes[i].w, shapes[i].h);
            }
        }
    }
    

    Each object created via new AnimationManager will get its own copy of the local variables within the constructor, which live on as long as any of the functions defined within the constructor is referenced anywhere. Thus the variables are truly private, and instance-specific. FWIW.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently started looking into Google Charts API for possible use within the
Is it possible to use an IF clause within a WHERE clause in MS
Possible Duplicate: Use SVN Revision to label build in CCNET I'm working through the
Is it possible to use a flash document embedded in HTML as a link?
Is it possible to use Apache Subversion (SVN) as general purpose backup tool? (As
Is it possible to use Microsoft Entity Framework with Oracle database?
Is it possible to use an UnhandledException Handler in a Windows Service? Normally I
Is it possible to use gcov for coverage testing of multi-threaded applications? I've set
Is it possible to use both JScript and VBScript in the same HTA? Can
Is it possible to use BackGroundWorker thread in ASP.NET 2.0 for the following scenario,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.