Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript stopwatch

Posted on 2011-03-04
2
Medium Priority
?
512 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Suggested Courses

926 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