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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:12:24+00:00 2026-05-25T22:12:24+00:00

In Javascript, I want to create a handler for a mouse click. Then, I

  • 0

In Javascript, I want to create a handler for a mouse click. Then, I want to be able to “busy-wait” for a few seconds before running the next line of code.* But in the “busy-wait”, I want to still be able to process the mouse click events.

Why will the following code run the while loop entirely and THEN activate the handler? (as in, why doesn’t the mouse click handler event ever get called in the middle of the busy-wait while loop?)

<html>
    <body>
        <p id="debugMessageElement"> </p>

        <script type="text/javascript">
            canvas=document.createElement("canvas")
            var ctx = canvas.getContext("2d");
            canvas.width = 840;
            canvas.height = 560;
            document.body.appendChild(canvas);

            var mouse_input = function(event){
                document.getElementById("debugMessageElement").innerHTML = event.pageX + ", " + event.pageY + "<br />"
            }

            canvas.onmousedown = mouse_input;

            timeallowed = 3
            start = Date.now()
            while(true){
                now = Date.now()
                delta = now - start
                if(delta >= timeallowed*1000){
                    document.write("" + timeallowed + " seconds has passed")
                    break;
                }
            }
        </script>
    </body>
</html>

*The reason that I’m designing my code like the above is ultimately because I want to do something like this:

for(p in person){
    for(t in person[p].shirts){
        busy_wait_5_seconds() //However, I want to process mouse clicks in these five seconds.
        //THEN move on to the next shirt... After five seconds...
    }
}

P.S. if you’re going to test this code, please note that I used the HTML5 canvas, so some browsers might not work?

  • 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-25T22:12:25+00:00Added an answer on May 25, 2026 at 10:12 pm

    JavaScript has only a single thread. If you do a while-true loop, it will essentially freeze the entire page until the code stops running.

    To wait a certain time before executing some code, you should use setTimeout. The final solution to your problem is a little more complicated than that though – you would need a recursive setTimeout to process the loop.

    edit: Here’s something I quickly whipped up which should solve your nested loop with waiting in the sub loop: http://jsfiddle.net/t4gsR/ – It may go a bit over your head if you’re still a beginner though, but press “run” on the top menu and see it work 🙂

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

Sidebar

Related Questions

I create a file in JavaScript. Now I want to be able to make
I want to create a compiled JavaScript file for my website. For development I
I want to create a string in JavaScript that contains all ascii characters. How
I want to create a draggable and resizable window in JavaScript for cross browser
Basically I want to create one large object of many object in JavaScript. Something
I'm not sure how I would do this in Javascript. I want to create
As the title says. I want to create TooltipDialog, after I click link and
I have a website on which I dynamically create Javascript code using ASP.NET handler
I have been tinkering with javasript (jquery) all day. I want to create a
I want to run javascript/Python/Ruby inside my application. I have an application that creates

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.