-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
79 lines (58 loc) · 1.52 KB
/
app.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
$(function () {
var result = $('.result'),
entry = $('.entry'),
calculation = $('.calculation'),
progressBar = calculation.find('.progressbar').progressbar({maximum: 10, step: 1}),
percent = 0;
progressBar.on('positionChanged', function (e) {
percent = e.percent;
});
var collectChoices = function () {
var inputs = entry.find('input[type="text"]'),
input,
i,
text,
choices = [];
for (i = 0; i < inputs.length; i++) {
input = $(inputs[i]);
text = input.val();
if (text !== '') {
choices.push(text);
}
}
return choices;
};
var calculateChoice = function () {
var choices = collectChoices(),
r = Math.floor(Math.random() * choices.length);
return choices[r];
};
var stepIt = function () {
progressBar.progressbar('stepIt');
if (percent >= 100) {
window.setTimeout(calculationDone, 1000);
return;
}
var r = Math.floor(Math.random() * 1000) + 300;
window.setTimeout(stepIt, r);
};
var startCalculation = function (e) {
e.preventDefault();
entry.addClass('invisible');
calculation.removeClass('invisible');
stepIt();
};
var calculationDone = function () {
result.find('blockquote').text(calculateChoice());
result.removeClass('invisible');
calculation.addClass('invisible');
progressBar.progressbar('setPosition', 0);
};
var restartEntry = function (e) {
e.preventDefault();
entry.removeClass('invisible');
result.addClass('invisible');
};
entry.find("form").submit(startCalculation);
result.find("form").submit(restartEntry);
});