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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:43:08+00:00 2026-06-08T06:43:08+00:00

Possible Duplicate: Javascript closure inside loops – simple practical example Calling setTimeout function within

  • 0

Possible Duplicate:
Javascript closure inside loops – simple practical example
Calling setTimeout function within a loop

before calling this a duplicate, I looked on the internet and everyone seemed to have different problems. If anybody could help me with this particular one, it’d be greatly appreciated.

Basicaly, what I have is two for loops nested inside a 3rd one.

for (a=0 ... a++) {
    for (b=0 ... b++) {
        setTimeout( ... + a + ..., 1000*b);
    }
    for (c=0 ... c++) {
        setTimeout( ... + a + ..., 1000*c);
    }
}

This is some pseudo code just to avoid the junk, but basically I want to pass the a value to the callback function triggered by the timer. The problem comes from the fact a is being incremented, so when then event is fired, the function has the last value of a instead of the one one it one registered.

I can think of it like a reference or a pointer in C/C++, this is really annoying. Any way to give it a permanent value?

  • 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-06-08T06:43:09+00:00Added an answer on June 8, 2026 at 6:43 am

    A closure in it’s simplest form would do the trick:

    for (a=0 ... a++) {
        (function(a){
           for (b=0 ... b++) {
               setTimeout( ... + a + ..., 1000*b);
           }
           for (c=0 ... c++) {
               setTimeout( ... + a + ..., 1000*c);
           }
        })(a);
    }
    

    To take Pointys answer into account: One should never pass a string to setTimeout.

    You can use the 3-parameters form of setTimeout and pass a function (or a reference to a function), the time value and the parameters for the function:

    for (a=0 ... a++) {
         for (b=0 ... b++) {
             setTimeout( function(x){ /*do stuff, using x */}, 1000*b, a);
         }
    }
    

    Note however, that unfortunately the 3 parameter form does not work in Internet Explorer.

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

Sidebar

Related Questions

Possible Duplicate: Javascript closure inside loops - simple practical example Javascript: closure of loop?
Possible Duplicate: Javascript closure inside loops - simple practical example How can I make
Possible Duplicate: Javascript closure inside loops - simple practical example I am using webservice
Possible Duplicate: Javascript closure inside loops - simple practical example Rather than explaining the
Possible Duplicate: Javascript closure inside loops - simple practical example Seen many posts talking
Possible Duplicates: Javascript closure inside loops - simple practical example Javascript closure “stores” value
Possible Duplicate: Event handlers inside a Javascript loop - need a closure? I have
Possible Duplicate: Javascript for loop and setTimeout issue I want this loop to change
Possible Duplicate: JavaScript scope and closure What is this for? (function(){ //The code to
Possible Duplicate: What is the 'new' keyword in JavaScript? creating objects from JS closure:

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.