-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (90 loc) · 2.64 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
var numbers = [];
var deb;
var fin;
var soundOn = true;
var backgroundSound;
$(function(){
backgroundSound = new Audio('background.mp3');
backgroundSound.play();
backgroundSound.loop = true;
backgroundSound.volume = 0.5;
init();
shuffle(numbers)
var counter = 0;
$('.box').each(function(btn){
$(this).text(numbers[counter]);
counter++;
});
var counter = 1;
var started = false;
$('.box').click(function(){
new Audio('click.wav').play();
if(started == false)
{
started = true;
deb = new Date();
timer();
$('.timer-container').fadeIn(800);
}
if(parseInt($(this).text()) == counter){
$(this).fadeOut(200);
$(this).removeClass('new');
counter++;
if(parseInt($(this).text()) == 24){
$('.info-container').append("<span class='number-item' >" + $(this).text() + "</span>");
}else{
$('.info-container').append("<span class='number-item' >" + $(this).text() + "</span>" + ",");
}
}
else{
new Audio('error.mp3').play();
$('.new').attr('background-color','red').fadeOut(500);
$('.new').attr('background-color','orange').fadeIn(500);
}
if(counter == 25){
new Audio('Congratulations.mp3').play();
new Audio('clap.mp3').play();
fin = new Date();
var diff = Math.abs(fin.getTime() - deb.getTime()) / 1000;
$('.result').text(diff + " seconds");
$('.result-container').fadeIn(2000);
$('.timer-container').fadeOut('2000');
}
});
// Sound button click
$('.sound-stop').click(function(){
if(soundOn == true){
backgroundSound.pause();
$(this).text("TURN ON SOUND");
soundOn = false;
}else{
backgroundSound.play();
$(this).text("TURN OFF SOUND");
soundOn = true;
}
});
});
function init(){
for(var i=0; i< 24; ++i)
{
numbers[i] = i+1;
}
}
function shuffle(a) {
var j, x, i;
for (i = a.length; i; i--) {
j = Math.floor(Math.random() * i);
x = a[i - 1];
a[i - 1] = a[j];
a[j] = x;
}
}
var timer = function startTimer(){
setInterval(function(){
var t = Math.abs(deb.getTime() - new Date().getTime());
var m = parseInt((t/1000)/60);
var s = parseInt(t/1000);
$('.timer-container').text(pad(m,2) + ":" + pad(s%60,2));
},1000);
}
function pad(a,b){return(1e15+a+"").slice(-b)}