]> bicyclesonthemoon.info Git - ott/bsta/blob - timer.js
improve GOTO handling
[ott/bsta] / timer.js
1 // timer.js
2 // 
3 // The countdown script.
4 //
5 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
6 //    Copyright (C) 2017, 2024  Balthasar SzczepaƄski
7 //
8 //    This program is free software: you can redistribute it and/or modify
9 //    it under the terms of the GNU Affero General Public License as
10 //    published by the Free Software Foundation, either version 3 of the
11 //    License, or (at your option) any later version.
12 //
13 //    This program is distributed in the hope that it will be useful,
14 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //    GNU Affero General Public License for more details.
17 //
18 //    You should have received a copy of the GNU Affero General Public License
19 //    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 // @license-end
21
22 var enabled = false;
23
24 window.onload = function () {
25         var e_h;
26         var e_m;
27         var e_s;
28         var h;
29         var m;
30         var s;
31         var countdown;
32         var timer;
33         
34         e_h = document.getElementById("ongh");
35         e_m = document.getElementById("ongm");
36         e_s = document.getElementById("ongs");
37         
38         h = +(e_h.innerHTML);
39         m = +(e_m.innerHTML);
40         s = +(e_s.innerHTML);
41         
42         timer = document.getElementById("timer");
43         timer.onclick = function () {
44                 enabled = !enabled;
45         }
46         
47         if (e_h == null || e_m == null || e_s == null) {
48                 // window.alert("NUL");
49         }
50         else {
51                 countdown = setInterval (function() {
52                         if(isNaN(h) || isNaN(m) || isNaN(s)) {
53                                 // window.alert("NAN");
54                                 clearInterval(countdown);
55                                 return;
56                         }
57                         
58                         if (s > 0) {
59                                 s -= 1;
60                         }
61                         else {
62                                 s = 59;
63                                 if (m > 0) {
64                                         m -= 1;
65                                 }
66                                 else {
67                                         m == 59;
68                                         if (h > 0) {
69                                                 h -= 1;
70                                         }
71                                         else {
72                                                 m = 0;
73                                                 s = 0;
74                                         }
75                                 }
76                         }
77                         
78                         if (enabled) {
79                                 if (h == 0 && m == 0 && s == 0) {
80                                         e_h.innerHTML = "00";
81                                         e_m.innerHTML = "00";
82                                         e_s.innerHTML = "NG";
83                                         // window.alert("ONG");
84                                         clearInterval(countdown);
85                                         return;
86                                 }
87                                 else {
88                                         e_h.innerHTML = ((h < 10) ? "0" : "") + h;
89                                         e_m.innerHTML = ((m < 10) ? "0" : "") + m;
90                                         e_s.innerHTML = ((s < 10) ? "0" : "") + s;
91                                 }
92                         }
93                 }, 1000);
94         }
95 };