Solved

Javascript stopwatch

Posted on 2011-03-04
2
464 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

777 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