www.webdeveloper.com
Results 1 to 3 of 3

Thread: javascript stopwatch

Hybrid View

  1. #1
    Join Date
    Dec 2009
    Posts
    4

    javascript stopwatch

    H there this is my first post! I wanted to ask if anyone could come up with something to solve my quite simple program. With the following code you can program a simple stopwatch, what I want to add is a button than when the stopwatch is stopped, and you click, that the recorded time is recorded and displayed elsewhere (without reloding)

    the code is the following...
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var c=0;
    var t;
    var timer_is_on=0;
    
    function timedCount()
    {
    document.getElementById('txt').value=c;
    c=c+1;
    t=setTimeout("timedCount()",1000);
    }
    
    function doTimer()
    {
    if (!timer_is_on)
      {
      timer_is_on=1;
      timedCount();
      }
    }
    
    function stopCount()
    {
    clearTimeout(t);
    timer_is_on=0;
    }
    </script>
    </head>
    
    <body>
    <form>
    <input type="button" value="Start count!" onClick="doTimer()">
    <input type="text" id="txt">
    <input type="button" value="Stop count!" onClick="stopCount()">
    </form>
    </body>
    </html>
    I&#180;m aware im quite a newbie to all this and that my question is quite simple and maybe to vague but I hope you can help me anyways

    Cheers to you all and thanks in advance

    Trufa
    Last edited by Kor; 12-22-2009 at 05:30 AM. Reason: wrap the code [code][/code]

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415

    Question

    Quote Originally Posted by jmvarelasudy View Post
    H there this is my first post! I wanted to ask if anyone could come up with something to solve my quite simple program. With the following code you can program a simple stopwatch, what I want to add is a button than when the stopwatch is stopped, and you click, that the recorded time is recorded and displayed elsewhere (without reloding)

    the code is the following...

    Code:
    // Put your code between the [ code] and [ /code] tags (without spaces)
    // to make it stand out and be easier to read by the forum members.

    Im aware im quite a newbie to all this and that my question is quite simple and maybe to vague but I hope you can help me anyways

    Cheers to you all and thanks in advance

    Trufa
    Unclear of the request ...

    Where is the 'elsewhere'?

    Same page?
    Pop-up <div> section?
    Opened in a new window?
    Passed to an existing page?

    What do you mean by 'recorded'?

    Saved to a file?
    Saved to a cookie?
    Passed to the server?

    BTW: Welcome to the forum!

  3. #3
    Join Date
    Dec 2009
    Posts
    4
    hey sorry about JMARKER and thanks for the advice, the thing is I have two big problems:
    1) English is not my language
    2) I&#180;m a COMPLETE newbie!!!!!!

    I could solve my problem in another forum anyway:

    I leave it in case anybody has the same doubt!

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var c=0;
    var t;
    var timer_is_on=0;
    var x=1;
    
    
    function timedCount()
    {
    document.getElementById('txt').value=c;
    c=c+1;
    t=setTimeout("timedCount()",1000);
    }
    
    function doTimer()
    {
    if (!timer_is_on)
      {
      timer_is_on=1;
      timedCount();
      }
    }
    
    function stopCount()
    {
    clearTimeout(t);
    timer_is_on=0;
    }
    
    function saveSeconds()
    {
       var seconds = document.getElementById('txt').value;
       if (!timer_is_on && seconds != '')
       {
            document.getElementById('save').value = document.getElementById('save').value + x++ + ":" + seconds + '\n';
            c=0;
            document.getElementById('txt').value = '';
       }
    }
    
    
    
    </script>
    </head>
    
    <body>
    <form>
    <input type="button" value="Start count!" onClick="doTimer()">
    <input type="text" id="txt">
    <input type="button" value="Stop count!" onClick="stopCount()">
    <input type="button" value="Save" onClick="saveSeconds()">
    <br>
    <textarea id="save" name="save" cols="10" rows="10"></textarea>
    </form>
    </body>
    </html>
    I have another question but will go in a new thread!

    Cheers! and thanks for the welcoming!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles