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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:10:11+00:00 2026-06-07T02:10:11+00:00

The code might look more complicated than it needs to be I want to

  • 0

The code might look more complicated than it needs to be I want to pass i into the balm function but it returns undefined because I’m doing it wrong.
Just extra information: This is for the server of a game I’m writing. Running in console for node.

for (i=30;i>=0;i--){
   setTimeout(function balm(i){
      this_sql ="UPDATE game_moblist SET hp = least(max_hp, hp +"+Math.round(i/2)+") WHERE id ="+mobid
      connection.query(this_sql, function(err, rows, fields) {if (err) err=null});
      console.log(this_sql)
      this_sql ="SELECT hp, max_hp FROM  game_moblist WHERE id ="+mobid;   //emite catch all update pointint to hp..
      connection.query(this_sql, function(err, rows, fields) {if (err) throw err;
      socket.emit ('updatemisc',handler,rows);//eval handler using args
      if (rows[0].hp==rows[0].max_hp){
         i=0;
         return i;
      }
      });
   },(31-i)*333);
}

here is a simplified version just showing the concept

for (i=3;i>=0;i--){
   setTimeout(function foo(i){
      console.log(foo)
   },1000*i);

I would expect the following output
“1” after 1000 ms
“2” after 2000 ms
“3” after 3000 ms
EDIT: It worked when I defined the function outside of setTimeout() and then called it like this
setTimeout(balm(i),…

  • 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-07T02:10:12+00:00Added an answer on June 7, 2026 at 2:10 am

    You can’t use a loop variable i declared outside the callback function and expect it to have the right value once the callback is actually executed – it’ll have the last value assigned to it instead.

    The code below shows the simplest (but not the shortest) solution:

    function showNumber(n) {
        return function() {
            console.log(n);
        }
    }
    
    for (i = 3; i >= 0; i--) {
         setTimeout(showNumber(i), 1000 * i);
    }
    

    In other words, you call a function (that has its parameter “bound” to your loop variable) which then returns another function that is the one actually invoked by setTimeout().

    There are other ways of doing this, typically with an Immediately Invoked Function Expression as shown in @Xander’s answer, but the code above demonstrates the solution nicely.

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

Sidebar

Related Questions

This question might look naive, but I couldn't understand this code in the ViewModelLocator.cs
I'm building a node server that needs to execute code that might be unsafe.
This might be nitpicky, but I like all my code to be in the
I have the following SQL design issue. The code below might look a little
I think my code might be written incorrectly and this is causing my problem.
We have some ancient Delphi code (might have even originated as Turbo Pascal code)
I have several columns that I'd like to search against. My code might not
How might one craft a good explanation of why the following code is not
I think I might be causing a bug in my code because I'm unclear
might be a silly question nonetheless: I'm playing around with the following code: $a='a';

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.