-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraffler.js
44 lines (37 loc) · 1.13 KB
/
raffler.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
function Raffler(attendees) {
this.attendees = attendees;
this.spins = 3;
this.speed = 50;
this.i = 0;
this.draw = 0;
this.elements = $();
var raffler = this;
(function() {
$.each(raffler.attendees, function(index, attendee) {
$('.attendees').append($('<div class="attendee"></div>').text(attendee));
});
raffler.elements = $('.attendees .attendee');
})();
this.start = function() {
raffler.i = 0;
raffler.draw = raffler.elements.length * raffler.spins + Math.round(Math.random()*raffler.elements.length);
raffler.next();
};
this.next = function() {
if (raffler.i === raffler.draw) {
$('.attendee:visible').addClass('drawn');
return;
}
var elementIndex = raffler.i % raffler.elements.length;
var element = $(raffler.elements[elementIndex]);
raffler.elements.hide();
element.show();
setTimeout(function() {
raffler.next();
}, raffler.speed);
if (elementIndex == 0) {
raffler.speed += 50;
}
raffler.i++;
};
}