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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:35:45+00:00 2026-06-18T15:35:45+00:00

I want to show an ongoing number with JavaScript. For this I have developed

  • 0

I want to show an ongoing number with JavaScript.

For this I have developed the following example:

for(var i=0; i<100; i++) {
  window.setTimeout(function() {
    alert(i);
  },1000*i);
}

Unfortunately the number 100 is shown every time. I think it is because i is a reference?

How can this be changed in the parameter passing?

  • 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-18T15:35:46+00:00Added an answer on June 18, 2026 at 3:35 pm

    The problem is that JS does not wait and proceeds finishing the loop before the timeouts expire. By then, i is already 100.

    To work around this issue, your timeout should have a local reference of i. That way, it’s not referencing the i that is already 100 by then, but references i at that time of the loop.

    for(var i=0; i<100; i++) {
      (function(i){
        //shadowing the loop-i with the function-i
        window.setTimeout(function() {
          //thus, this callback is referencing the i from the function
          //and not the loop's i
          alert(i);
        },1000*i);
      }(i));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have saved same image into /data/data/mypackage/img/ and now I want show this full
I'm using Drupal 7 and Views module in my site. And want show number
I want show my tool tip after 5 second mouse enter. I try following
I have some series on un-order lists, wrapped with <a href=#>Link</a> I want show
I have this code to show all post of category and thumbnail of them.
I have div which show the list whos online and I want show the
I have a little Problem with a MDX-Query . I want show all the
I have a paragraph <p>hello<br>wor&nbsp;l&nbsp;d</p> then I want show the paragraph's html in another
I want show text in textbox in 2 colors, for example 1 line red
i want show tooltip with image tag and i have written code like below

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.