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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:46:09+00:00 2026-06-04T14:46:09+00:00

Possible Duplicate: setTimeout in a for-loop and pass i as value i am trying

  • 0

Possible Duplicate:
setTimeout in a for-loop and pass i as value

i am trying generate dynamic array and using this array as loop .. but in loop settime out is not working or function is not working.
Here is my code

jQuery(document).ready(function () {
    temp = new Array();
    generateArray(temp);

    function generateArray(temp) {
        if (temp.length < 10) {
            res = randomXToY(1, 10, 0);
            for (var k = 0; k < temp.length; k++) {
                if (temp[k] == res) {
                    var test = 1;
                }
            }
            if (test != 1) {
                temp.push(res);
                //abc(temp);
            }
            generateArray(temp);
        } else {
            for (var z = 0; z < 10; z++) {
                tnest(temp[z]);
                setTimeout(function () {
                    removeClassImg(temp[z])
                }, 3000);
            }
            temp = new Array();
            generateArray(temp);
        }
    }

    function removeClassImg(result1) {
        alert(result1);
        $('#img' + result1).fadeTo(12000, 0.1);
        return true;
    }

    function tnest(result) {
        alert(result);
        $('#img' + result).fadeTo(12000, 1);
        return true;
    }

    function randomXToY(minVal, maxVal, floatVal) {
        var randVal = minVal + (Math.random() * (maxVal - minVal));
        return typeof floatVal == 'undefined' ? Math.round(randVal) : randVal.toFixed(floatVal);
    }
});

alert in function removeClassImg is not working .. i am using settimeout in for loop this is not working fine.

  • 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-04T14:46:10+00:00Added an answer on June 4, 2026 at 2:46 pm

    It has something to do with the timeout and loops. you need to wrap it in a closure so that your timeout callback is bound to the value of z “at that time”.

    I also notice this after the loop:

    temp = new Array();
    generateArray(temp);
    

    by the time you wanted to operate your delayed operation, your array does not contain the values you need anymore. You already cleared them.

    try this:

    for (var z = 0; z < 10; z++) {
        (function (tz) {                  //"localize" temp[z] by creating a scope
            tnest(tz);                    //that makes temp[z] local. this is done
            setTimeout(function () {      //by creating an immediate function
                removeClassImg(tz)        //passing temp[z] into it. that way, the 
            }, 3000);                     //timeout receives a local temp[z] which
        }(temp[z]));                      //has the value of temp[z] "at that time"
    }
    

    Here’s a sample with the closure and a sample without it. After 3 seconds, you will see that the one without it will log all 10s instead of 0-10.

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

Sidebar

Related Questions

Possible Duplicate: setTimeout and “this” in JavaScript I am trying to put a timeout
Possible Duplicate: javascript: pause setTimeout(); Im using jQuery and working on a notification system
Possible Duplicate: array_splice() for associative arrays How to add an array value to the
Possible Duplicate: Is there ever a good reason to pass a string to setTimeout?
Possible Duplicate: function in setInterval() executes without delay I am trying to use setTimeOut
Possible Duplicate: Javascript: find the time left in a setTimeout()? I'm trying to use
Possible Duplicate: Array to named variables How can I convert an array like this
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: jQuery .load method not firing on IE9 In IE7 and IE8 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.