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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:15:53+00:00 2026-05-21T21:15:53+00:00

Here is the part of the code that matters: function drawCircle(i, color1, color2) {

  • 0

Here is the part of the code that matters:

function drawCircle(i, color1, color2) {
            var ctx = canvas.getContext("2d");
            if (i % 2 == 1) {
                ctx.fillStyle = color1;
            }
            else {
                ctx.fillStyle = color2;
            }
            ctx.beginPath();
            ctx.arc(110, 270, 10, 0, Math.PI * 2, true);
            ctx.closePath();
            ctx.fill();
        }
for (var i = 0; i < 10; i++) {
    setTimeout(drawCircle(i, color2, color5), 4000);
}

Note that this is just a snippet I wanted to try out before I use code similar to this on a larger project. This isn’t working properly, the image is only drawn once, that is, I only see the last circle that is drawn. I have googled this to death but nothing has helped me so far.

  • 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-21T21:15:54+00:00Added an answer on May 21, 2026 at 9:15 pm

    What you want to use is setInterval. setTimeout just fires the event once. They have the same argument list.

    Also I don’t think the way you use setTimeout is correct. The way you’ve written it now, the function is fired before actually passing anything to setTimeout/setInterval. So you should write:

    setInterval(function() {              // GOOD. An actual function is passed 
        drawCircle(...);                  // as the first argument.
    }, 4000);
    

    or:

    setInterval('drawCircle(...)', 4000); // GOOD. the JS statement is supplied as 
                                          // a string and later on will be evaluated 
                                          // and executed with eval().
    

    and NOT:

    setInterval(drawCircle(...), 4000);   // BAD! drawCircle() is fired, its result 
                                          // evaluated and passed on to setInterval 
                                          // that doesn't know what to do with it.
    

    EDIT

    You don’t have any blocking routines in JavaScript. It’s a single threaded, event driven language. If you call something like setInterval it succeeds immediately. Only after 4 seconds or so your callback function will be invoked. However in the meantime JS will be busy doing all sort of different stuff – like for example reacting to other events generated by the user. What you want to do is to call setTimeout once and then inside the callback function, just before returning invoke it again with the same function and a different set of arguments of i. Something along these lines:

    function drawCircle(i, ...) {
    
        // ... do stuff ...
    
        if (i < 10) {                    // Check if the callback has been invoked 10 
                                         // times already.
            setTimeout(function() {      // Schedule the callback for execution
                drawCircle(i + 1, ...);  // again after anoter 4 seconds.
            }, 4000);
        }
    }
    
    setTimeout(function() {              // Invoke the callback the first time 
        drawCircle(1, ...);              // after first 4 seconds.
    }, 4000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is part of my web.config for my WCF service: <bindings> <basicHttpBinding> <binding name=sslBinding>
In my JSF/Facelets app, here's a simplified version of part of my form: <h:form
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see
Here is the scenario: I'm writing an app that will watch for any changes
Here is a twofold question, with a theoretical part, and a practical one: When
I have a function that accepts an anonymous function as an argument and sets

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.