SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Document.write Problem

    Hello. I wish to document.write the system seconds at one second intervals onto document. But the script doesnt work properly. What am I doing wrong?

    PHP Code:
    function counter() {
    var 
    date=new Date();
    var 
    second=date.getSeconds();

    window.document.write(second);

    window.setTimeout("counter()",1000);
    }
    window.setTimeout("counter()",1000); 
    Thanks in advance.

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.write only works at loading time. You need to use the DOM api or innerHTML:
    HTML Code:
    <div id="counter"></div>
    <script>
    var handler = function() {
      document.getElementById("counter").innerHTML = (new Date()).getSeconds();
      setTimeout(handler, 1000);
    }
    handler();
    </script>

  3. #3
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Got it.

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What would be the benefit of defining the function as a variable?

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria View Post
    What would be the benefit of defining the function as a variable?
    Functions always are, in Javascript.
    I prefer this syntax, because it's explicit about that and because I can use the same syntax for all function declarations.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •