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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question can i get some help with a loop?

    I need to loop this timer. can i get some help with that?
    Code:
    <script> 
    <!-- 
    // 
     var milisec=0 
     var seconds=60 
     document.counter.d2.value='30' 
    
    function display(){ 
     if (milisec<=0){ 
        milisec=9 
        seconds-=1 
     } 
     if (seconds<=-1){ 
        milisec=0 
        seconds+=1 
     } 
     else 
        milisec-=1 
        document.counter.d2.value=seconds+"."+milisec 
        setTimeout("display()",100) 
    } 
    display() 
    --> 
    </script>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,148
    Thanks
    39
    Thanked 506 Times in 500 Posts

    Lightbulb

    Other than what looks to be an older script, what is it doing or not doing to your satisfaction?

    Note: It is recommended to use the ';' at the end of each line.
    Not mandatory, but will help keep things clear in the future.

    Also note the addition of some braces " { } " to clarify the if...else part of the logic.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title> Untitled </title>
    </head>
    <body>
    <form name="counter" action="#" method="post" onsubmit="return false">
    <input type="text" name="d2" value="">
    </form>
    
    <script> 
     var milisec=0; 
     var seconds=60; 
    // document.counter.d2.value='30'; // can't see purpose when seconds set to 60 ???
    
    function display(){ 
      if (milisec<=0){ milisec=9; seconds-=1; } 
      if (seconds<=-1){ milisec=0; seconds+=1; } else { milisec-=1; } 
      document.counter.d2.value=seconds+"."+milisec; 
      setTimeout("display()",100); 
    } 
    display() 
    </script>
    
    </body>
    </html>

  • Users who have thanked jmrker for this post:

    muldaria (01-20-2013)

  • #3
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Slightly Off-Topic: Standard JavaScript timers are horribly inaccurate – see the very left example here. The timer on the right is much more accurate, an explanation can be found here.

  • Users who have thanked Airblader for this post:

    muldaria (01-20-2013)

  • #4
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Im an intern for a website startup and my "boss" asked me to make a time that goes to 60 seconds infinitely. so can you somehow show me the code for that?

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    If by "loop" you mean execute each iteration of a loop at delayed intervals, then that isn't possible in JavaScript, nor should it be.

    That's why setTimeout and setInterval exist and your type of implementation must be used.

    Of course it could be improved, not least by ensuring that the function ceases to be called after it is no longer required.

  • #6
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Going completely off-topic now:

    When I started as an intern, the first thing they gave me was the book "Clean Code" – and at that point I already had ten years of programming experience. If you're going for making this your profession you really wanna read similar books ... even if you have to do it in your free time. The sooner you learn how to write quality code the easier it will be to take it in.

    As for your task: What exactly is your task? A timer that goes from 0 to 60 or from 60 to 0? Does it have to have tenths of seconds? Should it just repeat itself when it reached the end?

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,677
    Thanks
    80
    Thanked 4,644 Times in 4,606 Posts
    And a little on the side.

    Keeping in mind Airblader's warning about timer accuracy, at least you can simplify the code.

    There is no reason for separate variables for seconds and tenths-of-seconds.

    Oh, and your use of document.counter.d2.value would indicate you are using a named <form>, which is considered obsolete. So assuming you have a <form> with, properly, an id such as <form id="counter">:
    Code:
    var ticker = 0;
    var countField = document.getElementById("counter").d2;
    function tick( )
    {
        if ( (ticker-=0.1) <= 0 ) { ticker = 60; }
        countField.value = ticker.toFixed(1);
    }
    tick( );
    setInterval( tick, 100 );
    That counts DOWN from 60 to 0.1 before resetting to 60.

    If you wanted to go UP, from 0 to 59.9 before resetting to 0:
    Code:
    var ticker = 60;
    var countField = document.getElementById("counter").d2;
    function tick( )
    {
        if ( (ticker+=0.1) >= 60 ) { ticker = 0; }
        countField.value = ticker.toFixed(1);
    }
    tick( );
    setInterval( tick, 100 );
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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