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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:29:17+00:00 2026-05-26T23:29:17+00:00

Can someone break down what’s going on here clearly? function timerCheck() { for(var i=0;

  • 0

Can someone break down what’s going on here clearly?

function timerCheck() {
    for(var i=0; i<5; i++) {
        setTimeout(function() {
            console.log("Hello" + i);
        }, 3000);
    }
}

So as some of you may know, calling this function will not work as expected. What will end up happening is that this function will get called 5 times all at once with i set to 5 each time. This will be the output after 3 seconds:

Hello5
Hello5
Hello5
Hello5
Hello5

I also understand that using the setInterval method is the right way to approach this kind of problem, but I am curious what’s going on under the hood here. I really want to understand how Javascript works. Please note that I do not have a computer science background, just a self-taught coder.

  • 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-26T23:29:18+00:00Added an answer on May 26, 2026 at 11:29 pm

    This might help you understand what’s going on better:

    function timerCheck() {
        for(var i=0; i<5; i++) {
            console.log("Hi" + i);
            setTimeout(function() {
                console.log("Hello" + i);
            }, 3000);
            console.log("Bye" + i);
        }
    }
    

    You’ll see

    Hi0
    Bye0
    Hi1
    Bye1
    Hi2
    Bye2
    Hi3
    Bye3
    Hi4
    Bye4
    

    Immediately printed to the console, because all five iterations of the loop finish very quickly, and then after five seconds you’ll see:

    Hello5
    Hello5
    Hello5
    Hello5
    Hello5
    

    because the timeouts (which were all set at approximately the same time) all occur at once, and since the loop already finished: i == 5.

    This is caused by the scope of i. The variable i has a scope of everywhere after it is declared in timerCheck(); There is no local i inside your anonymous function in setTimeout set there is no var i, and i isn’t given as an argument to the function.

    You can fix this easily with a closure, which will return a function that has a local copy of i:

    function timerCheck() {
        for(var i=0; i<5; i++) {
            setTimeout((function(loc_i) {
                return function() {
                    console.log("Hello" + loc_i);
                };
            })(i), 3000);
        }
    }
    

    Which will output:

    Hello0
    Hello1
    Hello2
    Hello3
    Hello4
    

    To understand this:

    (function(loc_i) {
        return function() {
            console.log("Hello" + loc_i);
        };
    })(i)
    

    You have to know that a function can be executed immediately in Javascript. IE. (function(x){ console.log(x); })('Hi'); prints Hi to the console. So the outer function above just takes in an argument (the current value of i) and stores it into a local variable to that function called loc_i. That function immediately returns a new function that prints "Hello" + loc_i to the console. That is the function that is passed into the timeout.

    I hope that all made sense, let me know if you’re still unclear on something.

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

Sidebar

Related Questions

Can someone break down the syntax here. Please. I need to learn this ASAP.
Can someone please break down what a delegate is into a simple, short and
I hope someone can point out where I'm going wrong here. I'm trying to
Can someone please break down this code for me? I know it changes text
Can someone show me how to implement a recursive lambda expression to traverse a
Can someone explain the mechanics of a jump table and why is would be
Can someone give an example of a good time to actually use unsafe and
Can someone describe what a symbol table is within the context of C and
Can someone explain to me the advantages of using an IOC container over simply
Can someone explain this result to me. The first test succeeds but the second

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.