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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:24:57+00:00 2026-05-29T12:24:57+00:00

I am trying to run small snippet code in JavaScript, where I want to

  • 0

I am trying to run small snippet code in JavaScript, where I want to write on the web page simple hello world each 5 seconds. I think it must be ok, but no, still I got only first hello world and no more. Could you give me a hand in this? Thanks

 <script type="text/javascript">
 var i=0;

 function startTimer() {

    window.setTimeout('refresh()',5000);
 }

 function refresh() {

 document.write("Hello world "+i+"<br/>");
 i++;
 window.setTimeout('startTimer()',1);
 } 

 startTimer();
 </script>
  • 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-29T12:25:01+00:00Added an answer on May 29, 2026 at 12:25 pm

    NOTE: As Amar Palsapure has noted in this answer, the root cause of the problem was the use of document.write. In my demonstration, I use a p element to document.body.appendChild() to add the text to the screen.

    You can use setTimeout(), but you have to make it contingent on the last setTimeout() that ran; so that each time the caller runs, it creates the next timeout.

    setInterval() is designed to run at a “regular” interval (neither setTimeout() nor setInterval() are truly reliable in when they run); however, if the calls to setInterval() get backed up due to some other process blocking it’s execution (Javascript is single-threaded), you could have issues with those queued callbacks. That’s why I prefer the approach I have below.

    Note, refrain from the setTimeout('funcCalled()', 100) usage; this is running an eval() on that string you’re passing in, which can change the scope in which you’re running the callback, as well as being considered “evil” due to security issues related to eval(). You’re best to avoid it altogether.

    EDIT – Modified slightly.

    I have made some changes to the approach. See my comments.

    // The first and last lines comprise a self-executing,
    // anonymous function, eg, (function(){})();.
    // This allows me to use a local function scope and not the
    // global window scope, while still maintaining my variables
    // due to it being a "closure" (function(){}).
    (function(){
        var i = 0,
            timer = 5000,
            // I'm just going to add this to the DOM.
            text = document.createElement('p'); 
    
        // This is a variable function, meaning it stores a 
        // reference to a function.
        var helloWorld = function() {
            // Here is where I add the Hello World statement
            text.innerHTML += 'Hello World! Loop: ' + i++ + '<br/>';
            // Them add it to the DOM.
            document.body.appendChild(text);
    
            // I added this so it wouldn't run forever.
            if (i < 100) {
                // A setTimeout() will be added each time the last
                // was run, as long as i < 100.
                // Note how I handle the callback, which is the
                // first argument in the function call.
                setTimeout(helloWorld, timer);
            }
    
            // I added the change so it wouldn't take so long 
            // to see if was working.
            timer = 500;
        }
    
        // Here I use a variable function and attach it to the
        // onload page event, so it will run when the page is
        // done loading.
        window.onload = helloWorld;
    })();
    

    http://jsfiddle.net/tXFrf/2/

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

Sidebar

Related Questions

I was developing a small function when trying to run an enumerator across a
When trying to run an Eclipse Dynamic Web Project under a Tomcat setup using
I'm trying to run a batch file, as another user, from my web app.
Perhaps I'm over-simplifying this by only giving you a small snippet of code (and
I am trying to make a small html page including jquery . I added
We are trying to run SCRUM for a small development team (three and a
I'm currently trying to write a small service to delete one directory and all
I am having a small issue, I am trying to run a $(form#picform).submit after
I'm trying to run the following code on a website to set up the
Hi i have a small problem, first of all im trying to run 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.