首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >setTimeout会多次执行相同的函数

setTimeout会多次执行相同的函数
EN

Stack Overflow用户
提问于 2018-07-23 03:53:06
回答 1查看 50关注 0票数 0

我有这样的代码:

代码语言:javascript
复制
document.addEventListener("DOMContentLoaded", function(event) {
  // Select the node that will be observed for mutations
  var parentOfMyList = document.body;

  // Options for the observer (which mutations to observe)
  var config = {
    attributes: true,
    childList: true,
    subtree: true
  };

  // Callback function to execute when mutations are observed
  var callback = function(mutationsList) {
    for (var mutation of mutationsList) {
      if (mutation.type == 'childList') {

        if (document.getElementById("topcmm-123flashchat-main-toolbar-message-type-option") != null) {
          var elt = document.getElementById("topcmm-123flashchat-main-toolbar-message-type-option");
          setTimeout(elt.click.bind(elt), 2000);
          if (document.getElementById("topcmm-123flashchat-toolbar-style-send-sound-btn") != null) {
            var clic2 = document.getElementById("topcmm-123flashchat-toolbar-style-send-sound-btn");
            setTimeout(clic2.click.bind(clic2), 2100);
            if (document.getElementById("topcmm-123flashchat-send-message-panel-close-icon") != null) {
              var clic3 = document.getElementById("topcmm-123flashchat-send-message-panel-close-icon");
              setTimeout(clic3.click.bind(clic3), 2200);

              //execute some function
            }
          }
        }
      }
    }
  };

  // Create an observer instance linked to the callback function
  var observer = new MutationObserver(callback);
  observer.observe(parentOfMyList, config);
});

代码应该执行一些函数(为了方便起见没有包含在这里),这些函数只有在topcmm-123flashchat-sound-messages-contents出现在DOM中才能执行,只有当topcmm-123flashchat-toolbar-style-send-sound-btn出现并被单击时,它才会出现,而这个函数在topcmm-123flashchat-main-toolbar-message-type-option出现并被单击之前也不会出现在DOM中。

因此,我编写了上面的代码,以便自动逐个单击元素,并让它们出现在DOM中,这样就可以执行问题中的函数。

页面加载后大约3秒,topcmm-123flashchat-main-toolbar-message-type-option将自动出现,如果单击,则会出现topcmm-123flashchat-toolbar-style-send-sound-btn;如果单击该页面,则会出现topcmm-123flashchat-sound-messages-contents,并将执行该函数。另外,我还单击了topcmm-123flashchat-send-message-panel-close-icon,以便关闭之前单击时打开的面板。

这里的问题是,如果eltclick2被多次执行,而click3似乎没有被执行,面板就会一直打开。为什么会这样呢?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-23 22:39:29

您需要添加逻辑,以防止对先前添加的元素重复相同的操作。

我添加了一个保存元素的变量。第一次看到元素时,变量为空,因此运行if代码。它在以后的运行中会被跳过。

代码语言:javascript
复制
document.addEventListener("DOMContentLoaded", function(event) {
  // Select the node that will be observed for mutations
  var parentOfMyList = document.body;

  // Options for the observer (which mutations to observe)
  var config = {
    attributes: true,
    childList: true,
    subtree: true
  };

  var elt = null,
    clic2 = null,
    clic3 = null;
  // Callback function to execute when mutations are observed
  var callback = function(mutationsList) {
    for (var mutation of mutationsList) {
      if (mutation.type == 'childList') {
        if (!elt && (elt = document.getElementById("topcmm-123flashchat-main-toolbar-message-type-option"))) {
          setTimeout(elt.click.bind(elt), 2000);
        }
        if (!clic2 && (clic2 = document.getElementById("topcmm-123flashchat-toolbar-style-send-sound-btn"))) {
          setTimeout(clic2.click.bind(clic2), 2100);
        }
        if (!clic3 && (clic3 = document.getElementById("topcmm-123flashchat-send-message-panel-close-icon"))) {
          setTimeout(clic3.click.bind(clic3), 2200);
        }
      }
      break;
    }
  };

  // Create an observer instance linked to the callback function
  var observer = new MutationObserver(callback);
  observer.observe(parentOfMyList, config);

  // other code can go here
});

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51468755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档