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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:13:10+00:00 2026-05-17T21:13:10+00:00

my question is actually one of understanding – I have a working solution, I

  • 0

my question is actually one of understanding – I have a working solution, I just don’t understand how it works.

Okay, so – what I’m trying to do is adding a setTimeout in a loop, and passing a changing value through it. Example:

for (i=0;i<11;i++)
{
     setTimeout("alert(i)",1000);
}

If I understood correctly, this doesnt work because Javascript does not (like PHP) pass the value of i to the function, but passes a reference of i – which in turn is not static, but continues to change with the counter.

I found a solution, which goes like this:

for (i=0;i<11;i++)
{
    setTimeout(function(x){return function(){alert(x)};}(i),1000);
}

I don’t really understand what this actually does. It looks like it passes a the “alert” function back to the calling function, but I can’t make any sense of that.

I can work with this solution and also adapt it to other contexts, but I’d really like to understand all of my code, not just use stuff I found somewhere and be happy it works. And in addition, I’m looking for a slimmer version to achieve the same goal.

Thanks, Marco

  • 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-17T21:13:10+00:00Added an answer on May 17, 2026 at 9:13 pm

    The reason you’re calling a function that returns a function is that you need to have some way for the function being passed to setTimeout() to have a reference to the current value of i.

    Because the code waits to run for 1000ms, the for loop will be complete before it runs, and the value if i will be 11.

    But because a function has its own variable scope, you can pass the value of i into the function that is being called immediately, so that it is referenced by the local variable x, which the function being returned can reference when setTimeout() finally calls it.

    for (i=0; i<11; i++) {
        setTimeout(function(x){
                     // CONTINUE HERE:
                     // x is a local variable to the function being executed
                     //    which references the current value of i
    
                     // A function is being returned to the setTimeout that
                     //    references the local x variable
                     return function(){ alert(x); };
    
                   }(i) // START HERE:
                        // The "outer" function is executed immediately, passing the
                        //   current value of "i" as the argument.
         ,1000);
    }
    

    So you’re ending up with an equivalent that would be something like this:

    setTimeout( function(){ alert(x); }, 1000); //...where x === 0
    setTimeout( function(){ alert(x); }, 1000); //...where x === 1
    setTimeout( function(){ alert(x); }, 1000); //...where x === 2
    setTimeout( function(){ alert(x); }, 1000); //...where x === 3
    // etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Actually my question is all in the title. Anyway: I have a class and
I actually have an answer to my question but it is not parallelized so
Actually, this question seems to have two parts: How to implement pattern matching? How
I actually am quite embarrassed to ask such a question but it is one
The 'click sound' in question is actually a system wide preference, so I only
Important: This question isn't actually really an ASP.NET question. Anyone who knows anything about
Question: Is exception handling in Java actually slow? Conventional wisdom, as well as a
This is actually a two part question. First,does the HttpContext.Current correspond to the current
Disclaimer: This is not actually a programming question, but I feel the audience on
I've looked at every question so far and none seem to actually answer this

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.