-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
35 lines (26 loc) · 862 Bytes
/
script.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
const button = document.getElementById("button");
const toasts = document.getElementById("toasts");
const messages = [
"Message One",
"Message Two",
"Message Three",
"Message Four",
];
const types = ["info", "success", "error"];
button.addEventListener("click", () => createNotification());
function createNotification(message = null, type = null) {
const notif = document.createElement("div");
notif.classList.add("toast");
notif.classList.add(type ? type : getRandomType());
notif.innerText = message ? message : getRandomMessage();
toasts.appendChild(notif);
setTimeout(() => {
notif.remove();
}, 3000);
}
function getRandomMessage() {
return messages[Math.floor(Math.random() * messages.length)];
}
function getRandomType() {
return types[Math.floor(Math.random() * types.length)];
}