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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:56:16+00:00 2026-05-24T03:56:16+00:00

This is my code. What I want it to do is write 0, wait

  • 0

This is my code. What I want it to do is write 0, wait one sec, write 1, wait one sec, write 2, wait one sec, etc. Instead it writes 5 5 5 5 5

for(i = 0; i < 5; i++) {
    setTimeout("document.write(i + ' ')", 1000);
}

http://jsfiddle.net/Xb7Eb/

  • 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-24T03:56:17+00:00Added an answer on May 24, 2026 at 3:56 am

    1) You set all the timeouts to last 1 second at the same time. The loop doesn’t wait for the timeout to occur. So you have 5 timeouts that all execute at the same time.

    2) When the timeouts execute, the loop is long since complete and i has become 5. So once they execute, they all print “5”

    3) document.write() writes somthing onto the page, in the same place it executes. I.e. if you have <script>document.write("xyz")</script> in the middle of a piece of text, it’ll write “xyz” in the middle of the text. The timeouts, however, are not necessarily anywhere on the page. They exist only in code.

    Here’s a solution that’s as close to yours as possible: http://jsfiddle.net/rvbtU/1/

    var container = document.getElementById("counter");
    for(i = 0; i < 5; i++) {
        setTimeout("container.innerHTML += '" + i + " ';", 1000 * i);
    }
    

    However, that solution uses setTimeout’s ability to evaluate a string as javascript, which is never a good idea.

    Here’s a solution that uses an anymous function instead: http://jsfiddle.net/YbPVX/1/

    var container = document.getElementById("counter");
    var writer = function(number) {
        return function() { container.innerHTML += String(number) + " "; };
    }
    for(i = 0; i < 5; i++) {
        setTimeout(writer(i), 1000 * i);
    }
    

    Edit: Forgot to save the 2nd fiddle. Whoops. Fixed now.

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

Sidebar

Related Questions

The code that I want to write is like this: void MethodOnThreadA() { for
Is there a better way to write this code? I want to show a
I often find I want to write code something like this in C#, but
this is my code that I write it but I want to use LINQ
How do I write code to connect through Windows VPN client? I want this
I want to write in python the equivalent to this code in Matlab, zeta
I want to convert function object to function. I wrote this code, but it
before this I wrote all jquery-code into main fail. Now I want to move
First a little intro: Last year i wrote this http://dragan.yourtree.org/code/canvas-3d-graph/ Now, i want to
I have this code that I want to make point-free; (\k t -> chr

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.