Solved

Javascript stopwatch start and stop button

Posted on 2011-03-04
1
489 Views
Last Modified: 2012-08-13
Hey guys i have some code im writing for a stop watch.

At the moment it starts by clicking start but when you click stop it turns it to 00:00:00.00 rather then dsplaying the time stopped

<div id="container">
			<ul id="controls">
                <asp:Button ID="start" runat="server" Text="Start" />
			
			    <asp:Button ID="Stop" runat="server" Text="Stop" />
			    </li>
			</ul>
			<h2 id="timer">00:00:00.00</h2>
						<ul id="splits">
				<li></li>
			</ul>
		</div>
		
		
		</div>

Open in new window



Below is the javascript:
var $start = $('#start'),
$stop = $('#Stop'),
	startText = $start.text(),
	stopText = $start.attr('alternate'),
	$split = $('#split'),
	$reset = $('#reset'),
	$timer = $('#timer'),
	$splits = $('#splits'),
	/*
* I found this code on a few sites and am unsure of the original author.
* If you know please inform me so I can credit them here.
*
* 0 = start time
* 1 = end time
* 2 = state (stopped or counting)
* 3 = total elapsed time in ms
* 4 = timer (interval object)
* 5 = epoch (January 1, 1970)
* 6 = element (not used here, normally stores the DOM element to update with the time)
* 7 = split count
*/
	t = [0, 0, 0, 0, 0, 0, 0, 0],

	format = function (ms) {
	    var d = new Date(ms + t[5]).toString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, '$1');
	    var x = String(ms % 1000);
	    while (x.length < 3) {
	        x = '0' + x;
	    }
	    d += '.' + x;
	    return d.substr(0, d.length - 1);
	},

	zero = function (num) {
	    if (parseInt(num) < 0) var neg = true;
	    if (Math.abs(parseInt(num)) < 10) {
	        num = '0' + Math.abs(num);
	    }
	    if (neg) num = '-' + num;
	    return num;
	},

	start = function () {
	    t[t[2]] = (+new Date()).valueOf();
	    t[2] = 1 - t[2];

	    if (t[2] == 0) {
	        clearInterval(t[4]);
	        t[3] += t[1] - t[0];

	        $start.text(startText);

	        t[7]++;
	        $splits.show();
	        $('<li><span>' + zero(t[7]) + '</span> ' + format(t[3]) + '</li>').appendTo($splits).slideDown('fast');
	        $splits.find('li').removeClass('first last');
	        $splits.find('li:first').addClass('first').end().find('li:last').addClass('last');

	        t[4] = t[1] = t[0] = 0;

	        display();
	    }
	    else {
	        $stop.text(stopText);
	        t[4] = setInterval(display, 43);
	    }

	    return false;
	},

	reset = function () {
	    if (t[2]) {
	        start();
	    }

	    t[4] = t[3] = t[2] = t[1] = t[0] = 0;

	    display();

	    $start.text(startText);
	    $splits.slideUp('fast', function () {
	        $splits.empty();
	    });

	    t[7] = 0;

	    return false;
	},

	display = function () {
	    if (t[2]) {
	        t[1] = (new Date()).valueOf();
	    }

	    $timer.text(format(t[3] + t[1] - t[0]));
	},

	split = function () {
	    if (t[2] !== 0) {
	        t[7]++;
	        $splits.show();
	        $('<li><span>' + zero(t[7]) + '</span> ' + format(t[3] + t[1] - t[0]) + '</li>').appendTo($splits).slideDown('fast');
	        $splits.find('li').removeClass('first last');
	        $splits.find('li:first').addClass('first').end().find('li:last').addClass('last');
	    }

	    return false;
	},

	load = function () {
	    t[5] = new Date(1970, 1, 1, 0, 0, 0, 0).valueOf();

	    display();
	};

	$(function () {

	    $splits.empty();

	    load();

	    $start.click(start);
	    $stop.click(stop);
	    $split.click(split);
	    $reset.click(reset);


	    // Assign classes to special items
	    $('#controls li:last').addClass('last');

	});

Open in new window

0
Comment
Question by:runnerjp2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 15

Accepted Solution

by:
SRigney earned 500 total points
ID: 35037083
I don't see your stop method in there that is called when you click stop.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
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
Course of the Month10 days, 23 hours left to enroll

632 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