-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
66 lines (55 loc) · 1.68 KB
/
worker.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
var var_workTime, var_restTime, var_sets;
var signalActive, delayMin, delayMax, timeShow;
function timerWork() {
var timeleft = var_workTime;
var countdown = setInterval(function () {
timeleft--;
if (timeleft < 0) {
clearInterval(countdown);
self.postMessage(["stop_work"]);
timerRest();
}
else {
self.postMessage(["second_work", timeleft]);
}
}, 1000);
}
function timerRest() {
var timeleft = var_restTime;
var countdown = setInterval(function () {
timeleft--;
if (timeleft < 0) {
clearInterval(countdown);
self.postMessage(["start_work"]);
timerWork();
}
else {
self.postMessage(["second_rest", timeleft]);
}
}, 1000);
}
function selectSignal() {
var tmp;
do { //Ciclo per numero random
tmp = (Math.floor(Math.random() * 14));
} while (!signalActive[tmp]); //ATTENZIONE RISCHIO CICLO INFINITO
self.postMessage(["signal", tmp]);
var delayRandom = Math.floor(Math.random() * (delayMax - delayMin + 1)) + delayMin; //Metodo per trovare un ritardo randomico tra due input
setTimeout(selectSignal, delayRandom + timeShow);
}
self.addEventListener("message", function(event) {
if (event.data[0] == "timer") {
var_workTime = event.data[1];
var_restTime = event.data[2];
var_sets = event.data[3];
timerWork();
}
else if (event.data[0] == "signal") {
signalActive = event.data[1];
delayMin = event.data[2];
delayMax = event.data[3];
timeShow = event.data[4];
var delayRandom = Math.floor(Math.random() * (delayMax - delayMin + 1)) + delayMin; //Metodo per trovare un ritardo randomico tra due input
setTimeout(selectSignal, delayRandom + timeShow);
}
});