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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:45:08+00:00 2026-06-12T18:45:08+00:00

I have a small program that I am supposed to write that makes a

  • 0

I have a small program that I am supposed to write that makes a bouncy ball in a canvas. I can get a wireframe of a ball bouncing, but can’t seem to get the setTimeout to fire at all. I have read, read and read about the function, but can’t figure this out (new).

<!DOCTYPE HTML>
<html>
   <head>
      <title>basic Canvas</title>
      <style>
         #canvas1{
            border:1px solid #9C9898;
         }
         body{
            margin:0px;
            padding:0px;
        }
      </style>
      <script>
         function drawMe(){
            //Set x,y,radius
            var x = 60;
            var y = 60;
            var radius = 70;
            drawLoop(x,y,radius);
         }

         function drawLoop(x,y,radius){
            var canvas2=document.getElementById("canvas1");
            var ctx=canvas2.getContext("2d");
            for(i=1;i<100;i++){

                    if(y + radius >= canvas2.height){
                        d = 1;
                    }
                    if(y - radius <= 0){
                        d = 0;
                    }
                    if (d==0){
                        x = x + 10;
                        y = y + 10;
                    }
                    else if (d==1){
                        x = x + 10;
                        y = y - 10;
                    }
                    draw(x,y,radius);
                    window.setTimeout(function() {draw(x,y,radius)},3000);
                }
         }

         function draw(x,y,radius){
                    var canvas2=document.getElementById("canvas1");
                    var ctx=canvas2.getContext("2d");

                    ctx.beginPath();
                    ctx.arc(x,y,radius,0,2*Math.PI,false);
                    var gradient = ctx.createRadialGradient(x, y, 1, x, y, radius);
                    gradient.addColorStop(0,"blue");
                    gradient.addColorStop(1,"white");
                    ctx.fillStyle=gradient;
                    ctx.lineWidth=1;
                    ctx.strokeStyle="blue";
                    ctx.fill();
                    ctx.stroke();
         }
      </script>
   </head>
   <body onload="drawMe()">
      <canvas id="canvas1" width=1000" height="400">

      </canvas>
   </body>
</html>

A little function called ‘drawMe()’ which sets x, y, and radius, then calls a little drawing loop that fires 100 times that draws the bouncy ball (‘drawLoop’). at the bottom of the function drawLoop, I call draw, which actually drawls the circles. From what I’ve read, the line ‘setTimeout(function(){draw(x,y,radius)};,3000); should call the draw function every three seconds. But it doesn’t. What the heck am I doing wrong?

  • 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-12T18:45:09+00:00Added an answer on June 12, 2026 at 6:45 pm

    setTimeouts are counted from the time they are created. The loop runs almost instantly and creates the setTimeouts at almost the same time. They are then all ran 3 seconds later.

    One way to get around this is in the solution below. This does not increment the loop until the current timeout has been completed.
    http://jsfiddle.net/x8PWg/14/

    This is only one of the many potential solutions to this.

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

Sidebar

Related Questions

I have a small program that's supposed to sample some value from a device
I have a small program that I'm trying to create to get ip addresses
I have this small exercise that I am trying to finish, but I get
I have a small program that is suppose to count lexemes. What I get
I have a C# program that is supposed to pull data from a small,
I have a small program that reads in a file and processes the data
I have a small Java program that reads list of IPs from text file
I have a small java program that reads a file in, in eclipse i
I'm experimenting with file I/O. I have a small practice program that creates a
Below is a small program that I have written to count the number of

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.