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

The Archive Base Latest Questions

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

Please let me know if I’m coming at this block on a wrong angle.

  • 0

Please let me know if I’m coming at this block on a wrong angle. I have a series of functions I’d like to fire off, and I’d like to be able to set them all up in a loop.

for(var jj = 0; jj<monster.frames.length;jj++){
    setTimeout(
        functionName(jj),
        1000*jj
    );
}

The problem is that when that when functionName(jj) is exectuted, it’s being passed the value of jj which by that time has been changed to the last loop iteration 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-04T10:46:50+00:00Added an answer on June 4, 2026 at 10:46 am

    You need to ensure the inner function has a new variable for every iteration. The easiest way to do this is to create a self-executing anonymous function which receives the variable as an argument. You also need to fix the way you are calling the function – right now you register the return value of functionName(jj) as a callback. This would only be ok if that function actually returned a function.

    for(var jj = 0; jj<monster.frames.length;jj++){
        (function(jj) {
            setTimeout(
                function() { functionName(jj); },
                1000*jj
            );
        })(jj);
    }
    

    You can also use partial application to create a new function. However, old browsers do not support Function.prototype.bind so you’d have to add a shim for it.

    for(var jj = 0; jj<monster.frames.length; jj++){
        setTimeout(functionName.bind(this, jj), 1000*jj);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please let me know if you have any idea about it. Thanks EDIT What
Please let me know how to get the client IP address, I have tried
Please let me know if this is bad practice or in someway a bad
Please let me know if this has been asked before, I wasn't able to
Please let me know how to achieve this synchronization between the data while updating
Please let me know how to round a decimal number like 0.53124 to a
Please let me know how to bug fix this code . I tried and
Please let me know how can I complete this switch code: switch ($urlcomecatid) {
Please let me know if this should be on Server Fault... I've got some
(Please let me know if this is completely absurd, that is probably a reason

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.