Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2005
    Location
    London, UK
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Simple Clock, but slowed down!

    Hi guys,

    I'm trying to achieve this, but don't know how to go about it:
    All I need to do is write some javascript that displays a clock, in the format of HH:MM:SS that updates automatically every second. However, here's the twist:

    I need this clock to start at 7AM and gradually work its way up to 3.30PM across a period of (approx) two actual weeks. So in reality, every second on this clock should take about 40 seconds in real life. Would you say that this is possible?
    I wouldn't know where to start, so all help is greatly appreciated.

    Note: it doesn't have to take exactly two weeks. Give or take several hours, I don't mind.

    Many thanks!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    You cannot do this reliably using JavaScript which is purely client-side. You need a server-side langauge such as PHP or PERL to keep track of your artificial "time".

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    632
    Thanks
    1
    Thanked 21 Times in 21 Posts
    Is this what you need?
    Code:
        <script type="text/javascript">
          <!-- Hide this code from non-JavaScript browsers
            CurrentTime='070000';
            ST_delay=40000;
            function DoTime() {
              Now_S=CurrentTime%100;                    // seconds
              Now_M=Math.floor(CurrentTime/100)%100;    // minutes
              Now_H=Math.floor(CurrentTime/10000);      // hours
              if (Now_S < 10) { Now_S='0'+Now_S; }
              if (Now_M < 10) { Now_M='0'+Now_M; }
              if (Now_H < 10) { Now_H='0'+Now_H; }
              Output=Now_H+':'+Now_M+':'+Now_S;
              ST=document.getElementById('SlowTime');
              ST.innerHTML=Output;
              ST_timeout=setTimeout("DoTime();",ST_delay);
              Now_S++;
              if (Now_S > 59) {
                Now_M++;
                Now_S=0;
              }
              if (Now_M > 59) {
                Now_H++;
                Now_M=0;
              }
              if (Now_H > 12) {
                Now_H=1;
              }
              Now_S*=1;
              Now_M*=1;
              Now_H*=1;
              if (Now_S < 10) { Now_S='0'+Now_S; }
              if (Now_M < 10) { Now_M='0'+Now_M; }
              if (Now_H < 10) { Now_H='0'+Now_H; }
              CurrentTime=''+Now_H+Now_M+Now_S;
            } // DoTime
          // End hiding -->
        </script>
    
    The slow time is now 
    <span name="SlowTime" id="SlowTime">##:##:##</span>.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    This only works for one browser session. Each time the user revisits the site the "slow clock" is re-set to 07:00:00.

    A cookie could be used to keep track of the "time", but as I say this is not at all reliable as the cookie can be deleted (or altered) by the user. Or the user may not have JavaScript enabled, or may refuse to accept cookies ........

  • #5
    New Coder
    Join Date
    Jan 2005
    Location
    London, UK
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for the replies!
    I'm a master at server-side codes, it's just the JS that I'm pretty weak on.

    If you could possibly modify the code kindly provided by jalarie, and maybe put in caps TIME FROM SERVER-SIDE CODE or whatever is necessary

    Thanks again.


  •  

    Posting Permissions

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