This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Timer.js
69 lines (60 loc) · 1.67 KB
/
Timer.js
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
/**
* @file ModPE timer library for providing timer API.
* @author Astro <[email protected]>
* @version 1.0
* @license Apache-2.0
*/
/** @namespace global */
($ => {
"use strict";
const Thread_ = java.lang.Thread;
/**
* Class representing a timer.
* @since 2017-01-26
* @class
* @memberOf global
* @param {Number} [time=120] Time(sec)
*/
function Timer(time) {
this._isRunning = false;
this._time = time || 120;
}
Timer.prototype.getTime = function () {
return this._time;
};
Timer.prototype.setOnSecListener = function (func) {
this._onSec = func;
};
Timer.prototype.setOnStartListener = function (func) {
this._onStart = func;
};
Timer.prototype.setOnStopListener = function (func) {
this._onStop = func;
};
Timer.prototype.start = function () {
let thiz = this;
new Thread_({
run() {
if (typeof thiz._onStart === "function") {
thiz._onStart();
}
thiz._isRunning = true;
while (thiz._isRunning && thiz._time > 0) {
if (typeof thiz._onSec === "function") {
thiz._onSec(thiz._time);
}
Thread_.sleep(1000);
thiz._time--;
}
if (typeof thiz._onStop === "function") {
thiz._onStop();
}
thiz._isRunning = false;
}
}).start();
};
Timer.prototype.stop = function () {
this._isRunning = false;
};
$.Timer = Timer;
})(this);