I am encountering some issues with the clearTimeout() function.
The setTimeout() function is working as expected, but I want it to stop running when I close my notification.
I'm not sure what is causing the problem in my function.
After closing the notification, I am seeing this error message in my console:
Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
Thank you!
class Notification {
addNotification() {
let notificationContent = `Content <div onclick="notify.closeWindow(event)"></div>`;
let notifyArea = document.createElement("div");
notifyArea.classList.add("notification-area");
let notification = document.createElement("div");
notification.classList.add("notification");
notification.innerHTML = notificationContent;
const area = document.querySelector(".notification-area");
let firstTimer;
let secondTimer;
if (!area) {
document.body.appendChild(notifyArea);
notifyArea.appendChild(notification);
if (notification == null) {
clearTimeout(firstTimer);
} else if (notification) {
firstTimer = setTimeout(() => {
notifyArea.removeChild(notification);
}, 10000);
}
} else {
area.appendChild(notification);
if (!notification) {
clearTimeout(secondTimer);
} else {
secondTimer = setTimeout(function() {
area.removeChild(notification);
}, 10000);
}
}
closeWindow(e) {
e.target.parentElement.parentElement.remove();
}
}