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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:05:12+00:00 2026-06-16T00:05:12+00:00

I want to write a for loop which prints number 1 to 10 with

  • 0

I want to write a for loop which prints number 1 to 10 with intervals after every iteration “like this“

How can I achieve it? I tried sleep() setInterval() setTimeout(“) and what not but can’t seem to find any working solution. And if possible I would like to do this using pure Javascript only.

function abc(){
    for(i=1;i<=10;i++){
        document.write(i+"<br>");
        sleep(1000);
    }
}
  • 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-16T00:05:13+00:00Added an answer on June 16, 2026 at 12:05 am

    To answer the question, to get something like a sleep function you could just write somehting like this as a helper function

    function sleep(dur) {
     var d = new Date().getTime() + dur;
      while(new Date().getTime() <= d ) {
        //Do nothing
      }
    
    }
    
    console.log(new Date().getTime())
         sleep(1000)
    
     console.log(new Date().getTime())
    

    Then you could call the sleep function after every iteration like

    function abc(){
        for(i=1;i<=10;i++){
            document.write(i+"<br>");
            sleep(1000);
        }
    }
    

    But Note that sleep will freeze your browser in this time and
    you don’t really wan’t this kind of behaviour when you just want to periodiccally do sth

    window.setInterval would be what you want in such cases

        function abcd(i){
           document.write(i + "<br>")
        }
    
    
    function repeatedTimeout(func,times,duration) {
        var args = Array.prototype.slice.call(arguments).splice(3);
        var i = 0;
        args.push(i)
        var wrap = function () {
         if(args[args.length - 1] >= times)
           window.clearInterval(wrap)
          else {
    
             func.apply(this,args)
             args[args.length - 1]++
           }
        }
        window.setInterval(wrap,duration)
    }
    repeatedTimeout(abcd,10,1000)
    

    Which would call it 10 times every 1000 milliseconds, whithout freezing the Browers

    Heres the JSBin

    Update

    If it really has to be a for loop, you could do something like this,
    regardless of the sense it makes to me

    for (var i = 0; i <= 10 ; i++) {
      window.setTimeout(
        (function (i){ 
          return function() {
            document.write(i + "<br>")
          }
        })(i),i * 1000)
      }
    

    In this case heres another JSBin

    This would call window.setTimeout in a for loop and a multiple of the timeout with i as the timeout,
    this would work, but i’d rather suggest using setInterval like you already did in the Fiddle you posted in the comment

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

Sidebar

Related Questions

How can I write function which simulates while loop? It should takes 2 arguments:
I want to write some code which takes any positive, even number (greater than
I want write a simple query which will fetch data from a table (which
I want write a little code analyzer which parses nested structures and translates into
I would like to write an application which will stream data at 2400 baud
Can someone help me with this piece of code. In a loop I'm saving
Gurus, I want to know how to write a recursive function that prints 1
I have a code like this... if active == 1: // A flag which
I want to write a simple server socket in Ruby, which, when a client
Hello I want write my own desktop sharing application in Java. The application should

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.