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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:31:08+00:00 2026-05-22T21:31:08+00:00

I am trying to understand two things about this code: var updateFn = function(num){

  • 0

I am trying to understand two things about this code:

var updateFn = function(num){
   return function(){
      if(num == 6){
         console.info("100%, all items saved!")
      }
      else{
         var i = num/6;
         var pct = Math.round(100 * i);
         console.info(pct + "% saved");
      }
  };
};

for (var i = 1; i < 7; i++){
   setTimeout(updateFn(i), i * 500);
}
  1. According to what i have read about setTimeout() syntax;

    setTimeout("javascriptstatement",milliseconds);
    

    So, why do I have to increment the milliseconds each loop till the total time until 500*6 ms?
    Why doesn’t setTimeout(updateFn(i), 500); work as intended?

  2. Why do I have to return a function for the function passed as the first parameter of
    setTimeout?

    Why doesn’t this work?:

    var updateFn = function(num){
          if(num == 6){
             console.info("100%, all items saved!")
          }
          else{
             var i = num/6;
             var pct = Math.round(100 * i);
             console.info(pct + "% saved");
          }
    
    };
    
    for (var i = 1; i < 7; i++){
       setTimeout("updateFn(i)", i * 500);
    }
    

Thanks in advance.

  • 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-05-22T21:31:09+00:00Added an answer on May 22, 2026 at 9:31 pm
    1. It seems it is setting 6 timeouts to occur every 500ms. I think setInterval may be better here.

    2. You want to return a function because if you pass a string to setTimeout it gets evaled, and if you pass a function it just runs it.

    It seems this code is making a progress meter for a save operation, though assuming the save will take 3 seconds, and incrementing the counter every 1/2 second may not be the best idea.

    Anyway, instead of setting 6 timeouts, it would be better to use setInterval.

    var updateFn = function(num){
      if(num == 6){
         console.info("100%, all items saved!");
         clearInterval(saving);
      }
      else{
         var i = num/6;
         var pct = Math.round(100 * i);
         console.info(pct + "% saved");
      }
    };
    
    var count = 1
    var saving = setInterval(function(){
        updateFn(count++);
    }, 500);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand Ruby a bit better, I ran into this code surfing the
This is someone elses code that I am trying to understand and modify... The
So I am trying to understand two things: What are the differences between these
I am trying to understand difference between those two and really need a explanatory
I'm really trying to understand the difference between OpenID and OAuth? Maybe they're two
After trying to understand why client code is not rendered in a page (injected
Somewhere in my code I have: class aclass { ... function amethod() { $this->dom
I have been trying to understand a bit more about the wider picture of
I was trying to understand lesson 9 from NEHEs tutorial, which is about bitmaps
I'm a programming student trying to better understand pointers, one of the things I

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.