SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Just can't see the problem with this code

    Can anyone explain why the code below doesn't work??? It's really doing my nut. I was just trying to get my eye back in to JavaScript by creating a simple clock, but for some reason the setTimout function won't work ... so the seconds aren't going round. I've checked the format of the setTimout() on the internet and I can't see what's wrong with it:
    PHP Code:
    <html>

    <
    head>
    <
    title>JavaScript</title>
    <
    script>
    now=new Date()
    month=now.getMonth()
    today=now.getDate()

    function 
    doClock(){
    hours=now.getHours()
    mins=now.getMinutes()
    secs=now.getSeconds()
    totalTime=hours+":"+mins+":"+secs
    document
    .clockForm.time.value=totalTime;
    setTimeout('doClock()',1000)
    }
    </script>
    </head>

    <body onLoad=doClock()>
    <form name="clockForm">
    <input type="text" name="time">
    </form> 
    Oh and if it's something really stupid, then I'll just say it was a friend that coded it

  2. #2
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <html>
    <head>
    <title>JavaScript</title>
    <script>
    function doClock(){
    now=new Date()
    month=now.getMonth()
    today=now.getDate()
    hours=now.getHours()
    mins=now.getMinutes()
    secs=now.getSeconds()
    totalTime=hours+":"+mins+":"+secs
    document.clockForm.time.value=totalTime;
    setTimeout('doClock()',1000)
    }
    </script>
    </head>
    <body onLoad=doClock()>
    <form name="clockForm">
    <input type="text" name="time">
    </form>
    </body>
    </html>

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you both know, using:

    setTimeout( doClock, 1000 );

    is better than

    setTimeout( 'doClock()', 1000 );
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What did you change sylow?

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    He moved these three lines

    now=new Date()
    month=now.getMonth
    ()
    today=now.getDate()


    inside of the function doClock(). That's because at each iteration, you were still using the SAME Date object - which has a static time - which is why your display never updated.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh ok ... that makes sense. Thanks alot for your help guys


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
  •