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

  • Home
  • SEARCH
  • 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 6886757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:47:58+00:00 2026-05-27T05:47:58+00:00

window.onload = -> boxOrig1 = 10 boxOrig2 = 30 canvasW = 400 canvasH =

  • 0
window.onload = ->

  boxOrig1 = 10
  boxOrig2 = 30
  canvasW = 400
  canvasH = 300

  ctx = $("#canvas")[0].getContext('2d');

  draw = (origin,dimension) ->    
    ctx.clearRect(0, 0, canvasW, canvasH)
    ctx.fillStyle = 'rgb(200,0,0)'  
    ctx.fillRect(origin + boxOrig1, boxOrig1, dimension, dimension)  
    ctx.fillStyle = 'rgba(0, 0, 200, 0.5)'  
    ctx.fillRect(origin + boxOrig2, boxOrig2, dimension, dimension)

  for m in [10..100] by 10
    t = setTimeout (-> draw(m, 150)), 1000 
    t.clearTimeout
#     draw(m,150)
#     alert m

As an exercise, the code above is meant to draw a little design on a canvas, pause for a second, then redraw it again 10 pixels to the right.

I can see that the mechanics work fine when I interrupt the loop with an alert (as in those last two commented lines), but I’m not getting the expected behavior with the setTimeout function. The design just appears at the rightmost position after the timeout, skipping the incremental steps in between.

I’ve tried many different ways of doing this from other examples, but it’s just melting my brain. Any suggestions?

  • 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-27T05:47:59+00:00Added an answer on May 27, 2026 at 5:47 am

    Geoff has outlined one approach (using setInterval and clearing it from the callback), so I’ll outline the other: Using setTimeout from the callback. Something like

    m = 10
    do drawCallback = ->
      draw m, 150
      m += 10
      setTimeout drawCallback, 1000 unless m > 100
    

    Note that there is a subtle timing difference between the two approaches that you should be aware of: setInterval func, 1000 will run the function once every 1000ms; the chained setTimeout will put a 1000ms delay between each function call. So if draw took 100ms, say, the chained setTimeout would be equivalent to setInterval func, 1100. It probably doesn’t matter, but it’s worth being aware of.

    Bonus approach: You don’t have to abandon your loop; you could just set all the timeouts from it at once:

    for m in [10..100] by 10
      do (m) ->
        setTimeout (-> draw(m, 150)), 100 * m
    

    The do (m) is necessary so that the closure passed to setTimeout sees each value of m, not just its final value in the loop. See my article A CoffeeScript Intervention for more info on this.

    Finally: I know this all seems very confusing at first, but timing in JS is actually very simple because the language is single-threaded. That means that events you schedule with setTimeout or setInterval or any other async function will never occur during a loop, even if the loop is infinite. They only occur after all of your code has finished executing. I talk about this in a little more detail in my book on CoffeeScript.

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

Sidebar

Related Questions

I have a draft website with canvas perspective grid rendered by Javascript (window.onload event),
This is a crossbrowser window.onload script, I want to add a message, when the
What exactly is the difference between the window.onload event and the onload event of
I am having trouble with the window.onload and document.onload events. Everything I read tells
I'm trying to modify my GreaseMonkey script from firing on window.onload to window.DOMContentLoaded, but
I have this function in my head: <head> window.onload = function(){ var x =
function icPageInit() { $(icImgDiv + icAlternate()).setOpacity(0); return true; } window.onload = icPageInit; This piece
window.onload = function(){ var outerFn = function ( oParam ){ return oParam; } var
window.onload = function(){ var obj = '{ name : Raj, age : 32, married
I was wondering if the window.onload = function(){} (or any other kind of onload,

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.