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 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

I want to write a class which return NSMutableData, i have this code but
Typically I have to write layout code like this: <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=fill_parent android:orientation=vertical
I want this code to just work: def main(): c = Castable() print c/3
I want to write a paint program in the style of MS Paint. For
I can't understand why this code works: @ads = Ads.find( :all, :joins => INNER
I'm trying to find a solution for this problem. This is my example code:
Is there a nice concise way to write this logic in C#? if ((DateTime.Now
I have a script that write a iframe into the page. I want to
I'm trying to write a service in c# that should be run on a
i am downloading data from website using asynctask and my code for async task

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.