forked from solygambas/html-css-javascript-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
26 lines (21 loc) · 798 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
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"];
const getRandomMessage = () =>
messages[Math.floor(Math.random() * messages.length)];
const getRandomType = () => types[Math.floor(Math.random() * types.length)];
const 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);
};
button.addEventListener("click", () => createNotification());