setTimeout的不断执行相同的功能很多次(setTimeout keeps executing

2019-10-29 05:35发布

我有这样的代码:

 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出现并且被点击,并且这一个不会出现在DOM或者直到topcmm-123flashchat-main-toolbar-message-type-option出现,被点击,以及。

所以我写了上面的代码,以便自动点击之后另一个要素之一,并让它们出现在DOM所以问题的功能可以被执行。

topcmm-123flashchat-main-toolbar-message-type-option本身将出现约3秒后,页面加载后,如果点击则topcmm-123flashchat-toolbar-style-send-sound-btn会出现,如果一个被点击然后topcmm-123flashchat-sound-messages-contents将出现和功能将被执行。 我另外有topcmm-123flashchat-send-message-panel-close-icon ,以便有打开与前点击关闭面板点击。

这里的问题是面板保持开放,如果像eltclick2被执行多次,而click3似乎没有被执行。 这是为什么?

谢谢。

Answer 1:

您需要添加逻辑,以防止重复了以前添加的元素同样的动作。

我补充说,持有该元素的变量。 首次元件所看到的,所述可变空的,所以if代码运行。 它跳过未来的运行。

 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 }); 



文章来源: setTimeout keeps executing the same function many times