【发布时间】:2019-07-09 17:32:10
【问题描述】:
我遇到了 clearTimeout() 的一些问题。
setTimeout() 正在工作,但是当我关闭通知时,我希望 setTimeout 停止工作!
我的想法是我不知道我的函数中有什么不正确。
当我关闭通知时,我在控制台上收到了这个:
Uncaught DOMException: Failed to execute 'removeChild' on 'Node': 要移除的节点不是该节点的子节点。
谢谢!
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();
}
}
【问题讨论】:
标签: javascript settimeout removechild cleartimeout