source: timingandestimationplugin/branches/trac0.11/timingandestimationplugin/htdocs/StopwatchDisplay.js

Last change on this file was 7477, checked in by Russ Tyndall, 14 years ago

Made the stopwatch component less susceptible to browser flakiness and jsavascript slowdown. (Thanks T.R.Chuan)

File size: 2.3 KB
Line 
1/**
2 * Copyright (C) 2010, Tay Ray Chuan
3 */
4
5/*
6 * add buttons to control the stopwatch
7 *
8 * Button 'flow' has states: start -> stop <-> continue
9 * Button 'reset' has state: reset
10 */
11jQuery(function($) {
12    StopwatchDisplay = function() {
13        /* the stopwatch (looks like 00:00:00) */
14        var field_hour = $('<span>00</span>');
15        var field_min = $('<span>00</span>');
16        var field_sec = $('<span>00</span>');
17
18        var display = $('<div></div>')
19            .append(field_hour)
20            .append(':')
21            .append(field_min)
22            .append(':')
23            .append(field_sec);
24
25        field_hour = field_hour[0].firstChild;
26        field_min = field_min[0].firstChild;
27        field_sec = field_sec[0].firstChild;
28
29        var interval_id;
30        var start_time = null;
31            var end_time;
32            var total_time=0 ;
33            var now = function(){ return Math.floor((new Date()).getTime() / 1000);};
34        var interval_func = function() {
35                        var interval = (now() - start_time)+total_time;
36                        var h = 0, m = 0, s = 0;
37                        s = interval % 60;
38                        m = Math.floor(interval/60) % 60;
39                        h = Math.floor(interval/3600);
40                        field_sec.nodeValue = s < 10 ? '0'+s : s;
41                        field_min.nodeValue = m < 10 ? '0'+m : m;
42                        field_hour.nodeValue = h < 10 ? '0'+h : h;
43        };
44
45        return {
46            display: display,
47
48            pause_stopwatch: function() {
49                clearInterval(interval_id);
50                interval_id = null;
51                    total_time += now() - start_time;
52                    start_time = null;
53            },
54            continue_stopwatch: function() {
55                /*
56                 * We really want to do an add (of the seconds on the display)
57                 * - which is what we get when start_time is subtracted later.
58                 */
59                    start_time = now();
60                interval_id = setInterval(interval_func, 100);
61            },
62            reset_stopwatch: function() {
63                field_hour.nodeValue = '00';
64                field_min.nodeValue = '00';
65                field_sec.nodeValue = '00';
66                                start_time=null;
67                                total_time=0;
68            },
69            get_hours: function() {
70                                var total = total_time;
71                    if (start_time) total += (now() - start_time);
72                                return Math.round( ( total / 3600 ) * 100) / 100;
73            }
74        };
75    }();
76});
Note: See TracBrowser for help on using the repository browser.