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 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    stopwatch question

    i have a stopwatch which works:

    my showtime function is working by using setTimeOut. i have been told that itis not that accurate and does not take into account the execution time for the code. What i actually want to do is save the start time and then take the difference between the two when i update the stopwatch.

    my existing function is shown below. can anyone show me how to change this function to use the timing method i have described above?

    showtime = function(){
    seconds = seconds + 1
    if (seconds == 60)
    {
    seconds = 0;
    minutes = minutes + 1;
    }
    if (minutes == 60)
    {
    minutes = 0
    hours = hours + 1
    }
    var timeValuehr = "" + ((hours < 10) ? "0" : "") + hours;
    var timeValuemin = "" + ((minutes < 10) ? "0" : "") + minutes;
    var timeValuesec = "" + ((seconds < 10) ? "0" : "") + seconds;
    document.formTimeRec.elements['tbClock'].value = timeValuehr + " : " + timeValuemin + " : " + timeValuesec;
    timerID = setTimeout("showtime()", 1000);
    timerRunning = true;
    }

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Location
    underground
    Posts
    186
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not sure about this, but...

    Code:
    var sTime;
    function showtime()
     {
        if(!sTime)
          sTime = new Date();
        var nTime = new Date(new Date() - sTime);
        nTime = ("0" + nTime.getUTCHours() + " : 0" + nTime.getMinutes() + " : 0" + nTime.getSeconds()).replace(/0(\d{2})/g, "$1");
        document.formTimeRec["tbClock"].value = nTime;
        setTimeout(showtime, 1000);
      }
    Last edited by neofibril; 06-29-2004 at 06:42 PM. Reason: trim...


  •  

    Posting Permissions

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