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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:05:57+00:00 2026-06-15T04:05:57+00:00

Here what I’m trying to do. I’m having an array like the following var

  • 0

Here what I’m trying to do.
I’m having an array like the following

var my_array = ['1', '2', '3' ... ,'1000000000000000'];

What I want to do is create a bunch of HTML elements for every element of that array, and since the array can contain a huge number of elements I attempted to do the following so the browser won’t freeze.

for(var i in my_array)
{
    if(my_array.hasOwnProperty(i))
    {
       setTimeout(function(){
             do_something_with_data(my_array[i]);
       });
    }
 }

What happens though is that the my_array[i] within the setTimeout doesn’t have the value it should.

To be more accurate, when I try to console.log(my_array[i]) what I get is something like this:

"getUnique" function (){
   var u = {}, a = [];
   for(var i = 0, l = this.length; i < l; ++i){
      if(u.hasOwnProperty(this[i])) {
         continue;
      }
      a.push(this[i]);
      u[this[i]] = 1;
   }
   return a;
}

getUnique is a function I’ve added to the Array prototype just like this:

Array.prototype.getUnique = function(){
   var u = {}, a = [];
   for(var i = 0, l = this.length; i < l; ++i){
      if(u.hasOwnProperty(this[i])) {
         continue;
      }
      a.push(this[i]);
      u[this[i]] = 1;
   }
   return a;
};

Can please somebody help me with this issue?

  • 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-15T04:05:58+00:00Added an answer on June 15, 2026 at 4:05 am

    the setTimeout is executed after the loop is done, and i is the last key or some garbage value at that point. You can capture the i like so:

    for (var i in my_array) {
        if (my_array.hasOwnProperty(i)) {
            (function(capturedI) {
                setTimeout(function() {
                    do_something_with_data(my_array[capturedI]);
                });
            })(i);
        }
    }
    

    You should also not use for..in loops for arrays because it’s an order of magnitude slower (especially so with the .hasOwnProperty check) than a for loop and the iteration order is not defined

    If you have jQuery or willing to add some extra code for older browsers, you can do:

    my_array.forEach( function( item ) {
         setTimeout( function() {
             do_something_with_data( item );
         }, 1000);
    });
    

    With jQuery:

    $.each( my_array, function( index, item ) {
         setTimeout( function() {
             do_something_with_data( item );
         }, 1000);
    });
    

    See docs for [].forEach

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

Sidebar

Related Questions

Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here I am trying to change the value of the following check box while
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here is the code in a function I'm trying to revise. This example works
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id
Basically, what I'm trying to create is a page of div tags, each has
Here is an example: I write html code inside of textarea, then I swap
Here's what I'm trying to accomplish with this program: a recursive method that checks

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.