-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (103 loc) · 2.92 KB
/
index.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
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
$(document).ready(function() {
var stop = false;
var duration = 720;
var counter = null;
const audio = new Audio('sound.mp3');
function formatTime(seconds) {
var h = Math.floor(seconds / 3600),
m = Math.floor(seconds / 60) % 60,
s = seconds % 60;
if (h < 10) h = "0" + h;
if (m < 10) m = "0" + m;
if (s < 10) s = "0" + s;
return m + ":" + s;
};
function timer() {
duration--;
if (duration < 0) return clearInterval(counter);
$('#timer').html(formatTime(duration));
}
function imagePop(exerciseId) {
let $image = $(`#image${exerciseId}`);
if (exerciseId === 4 || exerciseId === 5) {
$image.show();
} else {
const positionX = Math.random() * ($('#exercise').outerWidth() - $image.outerWidth());
const positionY = Math.random() * ($('#exercise').outerHeight() - $image.outerHeight());
$image.css("top",`${positionY}px`);
$image.css("left",`${positionX}px`);
$image.show();
}
setTimeout(function () {
$image.hide();
}, 700);
};
function schedulePop(exerciseId) {
const delay = 1000 + (Math.random() * 2500);
setTimeout(function () {
if (stop === true) {
stop = false;
return;
}
if (exerciseId === 3) {
audio.play();
schedulePop(exerciseId);
} else {
schedulePop(exerciseId);
imagePop(exerciseId);
}
}, delay);
}
function launchExercise(exerciseId) {
$('#countdown').show();
$('h2, .button').hide();
setTimeout(function () {$('#countdown').text('3')},0);
setTimeout(function () {$('#countdown').text('2')}, 1000);
setTimeout(function () {$('#countdown').text('1')}, 2000);
setTimeout(function () {
$('#countdown').hide();
duration = 720;
$('#timer').text('12:00');
$('#timer').show();
counter = setInterval(timer, 1000);
if (exerciseId === 3) {
schedulePop(exerciseId);
} else if (exerciseId === 4 || exerciseId === 5) {
imagePop(exerciseId);
schedulePop(exerciseId);
} else {
imagePop(exerciseId);
schedulePop(exerciseId);
}
setTimeout(function() {
stop = true;
$('#exercise').hide();
$('#menu').show();
}, 720000);
}, 3000);
};
$('#exercise').hide();
$('.menu-button').on('click', function () {
$('#menu, h2, img,.button, #timer').hide();
const exerciseId = this.dataset.id;
$(`#state${exerciseId}`).show();
$(`#button${exerciseId}`).show();
$('#exercise').show();
});
$('#button1').on('click', function () {
launchExercise(1);
});
$('#button2').on('click', function () {
launchExercise(2);
});
$('#button3').on('click', function () {
audio.play();
launchExercise(3);
});
$('#button4').on('click', function () {
launchExercise(4);
});
$('#button5').on('click', function () {
launchExercise(5);
});
});