-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTinyTimer.js
More file actions
206 lines (205 loc) · 4.41 KB
/
TinyTimer.js
File metadata and controls
206 lines (205 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
*
* @Name : TinyTimer.js
* @Version : 1.0
* @Programmer : Max
* @Date : 2018-11-18
* @Released under : https://github.com/BaseMax/TinyTimerJs/blob/master/LICENSE
* @Repository : https://github.com/BaseMax/TinyTimerJs
*
**/
;(function(window,document)
{
"use strict";
/**
* @variable consts
*
* @goal : using in the whole of script
*
**/
const TIME_DELAY = 1000;//1s
const TIME_MUTATION= 1;//one
const KEY_DURATION = "data-timer-duration";
const KEY_UPDATE = "data-timer-update";
const KEY_FINISH = "data-timer-finish";
const KEY_DONE = "data-timer-done";
const KEY_REPEAT = "data-timer-repeat";
const KEY_DELAY = "data-timer-delay";
const KEY_INDEX = "data-timer-index";
const KEY_MUTATION = "data-timer-mutation";
/**
* @variable timers
**/
var timers=[];
/**
* @function stop
**/
function stop()
{
}
/**
* @function execute
**/
function execute(element)
{
if(element.hasAttribute(KEY_DURATION))//first necessary attribute for this library!
{
let timer_index=element.getAttribute("data-timer-index");
let duration=element.getAttribute(KEY_DURATION);
//Check it be a decimal number.
duration=parseInt(duration);
let update=null;
let done=null;//...
let finish=null;//...
let repeat=true;//...
let delay=TIME_DELAY;//default value
let mutation=TIME_MUTATION;//default value
if(element.hasAttribute(KEY_FINISH))
{
finish=element.getAttribute(KEY_FINISH);
}
if(element.hasAttribute(KEY_DONE))
{
done=element.getAttribute(KEY_DONE);
}
if(element.hasAttribute(KEY_UPDATE))
{
update=element.getAttribute(KEY_UPDATE);
}
if(element.hasAttribute(KEY_DELAY))
{
delay=element.getAttribute(KEY_DELAY);
if(typeof parseInt(delay) == "number")
{
delay=Math.floor(delay);
if(delay < 0)//min value is 0
{
delay=0;
}
}
else
{
delay=TIME_DELAY;
}
}
if(element.hasAttribute(KEY_MUTATION))
{
mutation=element.getAttribute(KEY_MUTATION);
if(typeof parseInt(mutation) == "number")
{
mutation=Math.floor(mutation);
if(mutation < 1)//min value is 1
{
mutation=1;
}
}
else
{
mutation=TIME_DELAY;
}
}
if(element.hasAttribute(KEY_REPEAT))
{
repeat=element.getAttribute(KEY_REPEAT);
//`true == "true"` is always false and not necessary to use from === operator.
if(repeat == "true")
{
repeat=true;
}
else if(repeat == "false")
{
repeat=false;//0
}
else if(typeof parseInt(repeat) == "number")
{
repeat=parseInt(repeat);
repeat=Math.floor(repeat);//e.g: convert 2.67 to 2
//if(repeat < 0)
if(repeat <= 0)
{
return;//Stop!
//repeat=0;
}
}
}
////////////////////////////////////////////////
var time = duration;
var minutes,seconds;
var index=1;
function updates()
{
if(time < 0)
{
if(done)
{
eval("window."+done+"("+timer_index+")");
}
//here === operator is necessary!
if(repeat === true || (typeof repeat == "number" && repeat > index))
{
time = duration;
}
else
{
if(finish)
{
eval("window."+finish+"("+timer_index+")");
}
clearInterval(timers[timer_index]);
//element.textContent = "";
return;//this is necessary!
}
index++;
}
minutes = parseInt(time / 60,10)
seconds = parseInt(time % 60,10);
//if(minutes < 10){minutes="0"+minutes;}
minutes = minutes < 10 ? "0" + minutes : minutes;
//if(seconds < 10){seconds="0"+seconds;}
seconds = seconds < 10 ? "0" + seconds : seconds;
if(update)
{
eval("window."+update+"("+timer_index+")");
}
element.textContent = minutes + ":" + seconds;
time-=mutation;
}
updates();//Elementary show of time
timers[timer_index]=setInterval(function()
{
updates();
},delay);
}
}
/**
* @struct timer
*
* @goal : access to public functions
*
* @return struct
**/
window.timer=
{
execute:execute,
timers:timers,
stop:stop,
};
/**
* @struct onload
*
* @goal : Execute and start timer...
*
* @return void
**/
window.addEventListener("load",function()
{
var data_items;
data_items = document.querySelectorAll("[data-timer-duration]");
data_items.forEach(function(item,index)
{
item.setAttribute(KEY_INDEX,index);
item.classList.add("timer-"+index);
execute(item);
});
},false);
}(window,document));