Timer ...

Discussion in 'JavaScript' started by redhits, Apr 17, 2008.

  1. #1
    How i can do an window.write but after 5 seconds?
     
    redhits, Apr 17, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can't use the document/window.write to write to the document after the document is closed (because it's no longer opened for writing!). You'd either have to do .write right as the page is loading or if you really want to wait 5 seconds after it's loaded, you'd have to inject whatever you want to write into a DOM element somewhere (such as a div).

    As for how to make a timer, use setTimeout(function, time in milliseconds)

    Example:
    
    <html>
      <head>
        <script type="text/javascript">
          function write() {
            document.getElementById('mydiv').innerHTML = "this was injected after 5 seconds";
          }
          setTimeout(write, 5000);
        </script>
      </head>
      <body>
        <div id="mydiv"></div>
      </body>
    </html>
    HTML:
    You can also just pass a function created on-the-fly such as
    setTimeout(function(){document.getElementById('mydiv').innerHTML = "this was injected after 5 seconds";}, 5000);
     
    zerxer, Apr 18, 2008 IP