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 8604055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:28:30+00:00 2026-06-12T02:28:30+00:00

Just starting out with canvas and javascript and can’t get my head around why

  • 0

Just starting out with canvas and javascript and can’t get my head around why the setTimeout in this snippet isn’t working. I initially thought it would trigger each frame since it’s contained within the loop. I have tried moving it around within the animate function as well to no avail.

$(document).ready(function(){
var canvas = $('#myCanvas');
var context = canvas.get(0).getContext('2d');   

var Shape = function(x1,y1,x2,y2){
    this.x1 = x1
    this.y1 = y1
    this.x2 = x2
    this.y2 = y2
}

var shapes = new Array();

shapes.push(new Shape(0,0,50,50));
shapes.push(new Shape(50,50,50,50));
shapes.push(new Shape(0,100,50,50));
shapes.push(new Shape(50,150,50,50));
shapes.push(new Shape(0,200,50,50));

function animate(){
    for (i=0;i<shapes.length;i++){
        context.clearRect(0,0,500,500);
        context.fillRect(shapes[i].x1,shapes[i].y1,shapes[i].x2,shapes[i].y2);
        setTimeout(animate, 500);
    };
};
animate();
});
  • 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-06-12T02:28:31+00:00Added an answer on June 12, 2026 at 2:28 am

    Something wrong in your animate().

    • Do not do setTimeout in a loop. That will freeze your browser.
    • The codes in for loop draw the rectangle and erase it immediately. That’s why you cannot see your animation.

    Consider changing your code like this.

      var i = 0;
      function animate(){
          context.clearRect(0,0,500,500);
          context.fillRect(shapes[i].x1,shapes[i].y1,shapes[i].x2,shapes[i].y2);
          i++;
          if (i == shapes.length) i = 0;
          setTimeout(animate, 500);
      };
      animate();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just starting out with canvas. I made this: http://www.kingoslo.com/instruments/ The arrow for
I am just starting out with Silverlight (2 RC0) and can’t seem to get
Just starting out, this should be a simple one but I haven't been able
I am just starting out on functions in PostgreSQL, and this is probably pretty
I'm just starting out using Lua, and I was wondering (because I can't find
I'm just starting out learning javascript, and tried to write a little script that
Just starting out with C#, so this may be overly simple that I keep
I'm just starting out with Java, but my boss is pushing for this. I've
Just starting out with LINQ to SQL (you can probably tell - be gentle).
Only just starting out with jQuery and I'm sure this is basic stuff for

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.