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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:45:00+00:00 2026-05-15T00:45:00+00:00

I thought I would try and be clever and create a Wait function of

  • 0

I thought I would try and be clever and create a Wait function of my own (I realise there are other ways to do this). So I wrote:

var interval_id;
var countdowntimer = 0;

function Wait(wait_interval) {
  countdowntimer = wait_interval;

  interval_id = setInterval(function() {
    --countdowntimer <=0 ? clearInterval(interval_id) : null;
  }, 1000);

  do {} while (countdowntimer >= 0);
}

// Wait a bit: 5 secs
Wait(5);

This all works, except for the infinite looping. Upon inspection, if I take the While loop out, the anonymous function is entered 5 times, as expected. So clearly the global variable countdowntimer is decremented.

However, if I check the value of countdowntimer, in the While loop, it never goes down. This is despite the fact that the anonymous function is being called whilst in the While loop!

Clearly, somehow, there are two values of countdowntimer floating around, but why?

EDIT

Ok, so I understand (now) that Javascript is single threaded. And that – sort of – answers my question. But, at which point in the processing of this single thread, does the so called asynchronous call using setInterval actually happen? Is it just between function calls? Surely not, what about functions that take a long time to execute?

  • 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-15T00:45:00+00:00Added an answer on May 15, 2026 at 12:45 am

    There aren’t two copies of the variable lying around. Javascript in web browsers is single threaded (unless you use the new web workers stuff). So the anonymous function never has the chance to run, because Wait is tying up the interpreter.

    You can’t use a busy-wait functions in browser-based Javascript; nothing else will ever happen (and they’re a bad idea in most other environments, even where they’re possible). You have to use callbacks instead. Here’s a minimalist reworking of that:

    var interval_id;
    var countdowntimer = 0;
    
    function Wait(wait_interval, callback) {
        countdowntimer = wait_interval;
    
        interval_id = setInterval(function() {
            if (--countdowntimer <=0) {
                clearInterval(interval_id);
                interval_id = 0;
                callback();
            }
        }, 1000);
    }
    
    // Wait a bit: 5 secs
    Wait(5, function() {
        alert("Done waiting");
    });
    
    // Any code here happens immediately, it doesn't wait for the callback
    

    Edit Answering your follow-up:

    But, at which point in the processing of this single thread, does the so called asynchronous call using setInterval actually happen? Is it just between function calls? Surely not, what about functions that take a long time to execute?

    Pretty much, yeah — and so it’s important that functions not be long-running. (Technically it’s not even between function calls, in that if you have a function that calls three other functions, the interpreter can’t do anything else while that (outer) function is running.) The interpreter essentially maintains a queue of functions it needs to execute. It starts starts by executing any global code (rather like a big function call). Then, when things happen (user input events, the time to call a callback scheduled via setTimeout is reached, etc.), the interpreter pushes the calls it needs to make onto the queue. It always processes the call at the front of the queue, and so things can stack up (like your setInterval calls, although setInterval is a bit special — it won’t queue a subsequent callback if a previous one is still sitting in the queue waiting to be processed). So think in terms of when your code gets control and when it releases control (e.g., by returning). The interpreter can only do other things after you release control and before it gives it back to you again. And again, on some browsers (IE, for instance), that same thread is also used for painting the UI and such, so DOM insertions (for instance) won’t show up until you release control back to the browser so it can get on with doing its painting.

    When Javascript in web browsers, you really need to take an event-driven approach to designing and coding your solutions. The classic example is prompting the user for information. In a non-event-driven world, you could do this:

    // Non-functional non-event-driven pseudo-example
    askTheQuestion();
    answer = readTheAnswer();      // Script pauses here
    doSomethingWithAnswer(answer); // This doesn't happen until we have an answer
    doSomethingElse();
    

    That doesn’t work in an event-driven world. Instead, you do this:

    askTheQuestion();
    setCallbackForQuestionAnsweredEvent(doSomethingWithAnswer);
    // If we had code here, it would happen *immediately*,
    // it wouldn't wait for the answer
    

    So for instance, askTheQuestion might overlay a div on the page with fields prompting the user for various pieces of information with an “OK” button for them to click when they’re done. setCallbackForQuestionAnswered would really be hooking the click event on the “OK” button. doSomethingWithAnswer would collect the information from the fields, remove or hide the div, and do something with the info.

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

Sidebar

Ask A Question

Stats

  • Questions 396k
  • Answers 396k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Update: My bad. I misread this as case insensitive search.… May 15, 2026 at 3:00 am
  • Editorial Team
    Editorial Team added an answer You should probably mention that "web hooks" is a specific… May 15, 2026 at 3:00 am
  • Editorial Team
    Editorial Team added an answer Microsoft sometimes will give a complimentary MSDN subscription to open… May 15, 2026 at 3:00 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.