admin管理员组文章数量:1303327
I'm using MutationObserver
to wait for the creation of an element on the page, and then adding a button on it (with init
function).
I only need to add one button but mutations keep happening after this.
I would like to disconnect()
the observer after having added this button.
I tried something like this:
function detect_node_for_buttons(mutations){
var selector = 'div[class="_2o3t fixed_elem"]';
mutations.forEach(function (mutation){
var element = $(document).find(selector);
if (element){
init();
observer.disconnect();
return;
}
if (!mutation.addedNodes) return;
for (var i = 0; i < mutation.addedNodes.length; i++){
if (mutation.addedNodes[i].matches(selector)){
init();
observer.disconnect();
}
}
});
}
var observer = new MutationObserver(function (mutations){
detect_node_for_buttons(mutations);
});
But it didn't work (Perhaps because observer
isn't yet defined when I call observer.disconnect()
in detect_node_for_buttons()
)?
How could I do it?
I'm using MutationObserver
to wait for the creation of an element on the page, and then adding a button on it (with init
function).
I only need to add one button but mutations keep happening after this.
I would like to disconnect()
the observer after having added this button.
I tried something like this:
function detect_node_for_buttons(mutations){
var selector = 'div[class="_2o3t fixed_elem"]';
mutations.forEach(function (mutation){
var element = $(document).find(selector);
if (element){
init();
observer.disconnect();
return;
}
if (!mutation.addedNodes) return;
for (var i = 0; i < mutation.addedNodes.length; i++){
if (mutation.addedNodes[i].matches(selector)){
init();
observer.disconnect();
}
}
});
}
var observer = new MutationObserver(function (mutations){
detect_node_for_buttons(mutations);
});
But it didn't work (Perhaps because observer
isn't yet defined when I call observer.disconnect()
in detect_node_for_buttons()
)?
How could I do it?
Share edited Sep 17, 2021 at 6:46 UserScripters Bot 416 bronze badges asked Sep 2, 2015 at 13:35 vmontecovmonteco 15.5k17 gold badges59 silver badges90 bronze badges 2-
Are you saying
detect_node_for_buttons
is triggered again, orinit
is triggered again?.disconnect
will stopdetect_node_for_buttons
from running, but if there were multiple items inmutations
or.addedNodes
then you'll still keep looping over them. – loganfsmyth Commented Sep 2, 2015 at 18:04 - @loganfsmyth I'm waiting for the creation of a single element. but the mutationobserver (and therefore detect_node_for_buttons) is still triggered that's right. I just need to execute it once. Now I think it's dirty and I would like to "clean" the mutationobserver after the function execution. – vmonteco Commented Sep 2, 2015 at 22:21
1 Answer
Reset to default 11Callback function in MutationObserver
is called with two arguments: mutations array and observer object itself. Because of how you wrote the code detect_node_for_buttons
never gets observer object. This will do the trick:
var observer = new MutationObserver(detect_node_for_buttons);
You also have to add this argument to function declaration:
function detect_node_for_buttons(mutations, observer){ ... }
本文标签: javascriptHow to disconnect a MutationObserver from inside its callback functionStack Overflow
版权声明:本文标题:javascript - How to disconnect a MutationObserver from inside its callback function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741692584a2392816.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论