I m making a Toast Notification.
I hope that When I click the 'Toast Notification' button, notification message come to on the bottom side and after a while, the messages disappear.
So I set the disappearing time to 5s , then after clicking the button and later 5 second , the messages are disappeared in sequence.
But the disappearing time was not the 5second except the first disappearing .
Time is getting faster and faster that is disappeared . It's not 5 second what I set before
My english is bad so I hope you understand by running code below
let toast = document.querySelector('.toast');
let notification = document.querySelector(".notification")
let sec = 5000;
toast.addEventListener('click', () => {
let newDiv = document.createElement("span");
let newText = document.createTextNode("No Way!");
newDiv.classList.add("notification_word");
newDiv.appendChild(newText);
notification.appendChild(newDiv);
if (document.querySelector(".notification_word")) {
setInterval(function() {
removeNotification()
}, sec);
}
})
function removeNotification() {
document.querySelectorAll(".notification_word")[0].remove()
}
body {
text-align: center;
justify-content: center;
align-items: center;
}
button.toast {
position: absolute;
top: 50%;
left: 50%;
width: 200px;
height: 100px;
font-size: 20px;
background: rgb(215, 188, 240);
margin: -100px 0 0 -100px;
border: 1px solid rgb(28, 3, 51);
border-radius: 20px;
cursor: pointer;
}
button.toast:hover {
background: rgb(178, 157, 201);
}
.notification {
display: flex;
flex-direction: column-reverse;
position: absolute;
right: 2%;
bottom: 2%;
}
span.notification_word {
background-color: rgb(196, 77, 243);
padding: 10px 0;
margin: 3px;
width: 100px;
border: 1px solid rgb(28, 3, 51);
border-radius: 5px;
}
<button class="toast">Toast Notification</button>
<div class="notification">
</div>