Solved

Javascript stopwatch

Posted on 2011-03-04
2
471 Views
Last Modified: 2012-05-11
Hey guys... im tryin to make alterations to my stopwatch but im stuck.

I want it to do this...

Show the time so at it just adds one but i want it to be in hh:mm.ss
I also would like to record time started and finished so:
04/03/11 11:49 - 12:00

so records the data and time it started and time it stopped so the outcome will be:


04/03/11 11:49 - 12:00    00:12.23


below is my current 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

Open in new window

0
Comment
Question by:runnerjp2005
2 Comments
 
LVL 14

Expert Comment

by:Dhanasekaran Sengodan
ID: 35035458
look this link

http://www.sivamdesign.com/scripts/clocks.html

hopes its helps u!!!!!
0
 
LVL 9

Accepted Solution

by:
jkofte earned 500 total points
ID: 35035668
I also include

var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
var currentSeconds = currentTime.getSeconds();

so that you may display them if you need to.

<html>
<head>
<script type="text/javascript">
var c=0;
var t;
var timer_is_on=0;
var x=1;


function timedCount()
{
var currentTime = new Date();
var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
var currentSeconds = currentTime.getSeconds();
document.getElementById('txt').value=currentTime;
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!

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question