【问题标题】:Why clearTimeout is not working in my code? javascript为什么 clearTimeout 在我的代码中不起作用? javascript
【发布时间】: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


    【解决方案1】:

    closeWindow 中清除您的计时器,否则将在删除节点后调用removeChild 函数。请注意,您必须将计时器设为类的属性才能在closeWindow 函数中访问它们

    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");
    
        this.firstTimer;
        this.secondTimer;
    
        if (!area) {
          document.body.appendChild(notifyArea);
          notifyArea.appendChild(notification);
    
          if (notification == null) {
            clearTimeout(this.firstTimer);
          } else if (notification) {
            this.firstTimer = setTimeout(() => {
              notifyArea.removeChild(notification);
            }, 10000);
          }
        } else {
          area.appendChild(notification);
    
          if (!notification) {
            clearTimeout(this.secondTimer);
          } else {
            this.secondTimer = setTimeout(function() {
              area.removeChild(notification);
            }, 10000);
          }
        }
    
      closeWindow(e) {
        clearTimeout(this.firsTimer);
        clearTimeout(this.secondTimer);
        e.target.parentElement.parentElement.remove();
      }
    }
    

    【讨论】:

    • 我只是添加了 notification.remove() 而不是 notifyArea.removeChild(notification);
    • 很好,但最好的解决方案是像这个答案一样清除超时。请接受答案。您可以使用this.firstTimer = setTimeout(function...,这样您就可以访问closeWindow 函数中的计时器
    • @BelowtheRadar 请将其添加到您的答案中,如果不将计时器 id 作为属性,它将无法工作
    猜你喜欢
    • 2015-09-28
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2021-08-14
    • 2018-12-01
    • 1970-01-01
    • 2016-03-02
    相关资源
    最近更新 更多