-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
81 lines (68 loc) · 1.88 KB
/
main.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
/**
* JavaScript Principal
*/
import { players } from './storage.js'
import { getNumberInt } from './utils/get-number-int.js'
import { sleep } from './utils/sleep.js'
import { button, container, number, title, raffle, color } from './elements.js'
const timeout = 5000 // 5 segundos
var url_string = window.location.href
var url = new URL(url_string)
if(url.searchParams.get('title') != (null || undefined)) {
title.innerText = url.searchParams.get('title') // obtem query string
}
if(localStorage.getItem('color') != undefined) {
raffle.style = `background-color: ${localStorage.getItem('color')};`;
}
const showWinner = (name) => {
const custom = `
font-size: 22px;
color: #2a1e5c;
font-weight: bold;
`
const p = document.createElement('p')
p.setAttribute('style', custom)
p.innerHTML = `Parabéns ${name}!`
container.appendChild(p)
setTimeout(() => {
number.innerHTML = 0
p.remove()
}, timeout + 10000)
}
const getNameWinner = (valueNow) => {
players.forEach((player) => {
player.numbers.forEach((number) => {
if (number === valueNow) {
showWinner(player.name)
}
})
})
}
const loader = () => {
const gif = document.createElement('img')
gif.setAttribute('src', './assets/images/loading.gif')
gif.setAttribute('width', '50')
number.innerHTML = ''
number.appendChild(gif)
}
const confetti = () => {
const gif = document.createElement('img')
gif.setAttribute('src', './assets/images/confete.gif')
gif.setAttribute('width', '100%')
gif.setAttribute('style', 'position: absolute;')
container.appendChild(gif)
setTimeout(() => {
gif.remove()
}, timeout)
}
button.addEventListener('click', async () => {
const response = getNumberInt(1, 50)
const pause = 500 // meio segundo
setTimeout(() => {
number.innerHTML = response
getNameWinner(response)
sleep(pause)
confetti()
}, timeout)
loader()
})