我试图等到 Mutation 事件开始覆盖它刚收到的值。在我尝试正常应用它之前,它一直被我无法控制的第 3 方提供的其他值覆盖。
/** Intercept changes in select2-drop positioning **/
var MutationObserver = window.WebKitMutationObserver;
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log("Override existent new found top value with -> " + newHeight);
$(".select2-drop-active").css({'top':newHeight+"px"});
});
});
在我尝试这段代码后,我认为它进入了一个循环,导致我的 Chrome 卡住并且内存开始快速增加
有没有办法停止 Mutations 中的连续调用?
最佳答案
您可以随时调用 observer.disconnect 暂停您的突变观察器
因此,根据您的用例,您可以执行如下操作:
var MutationObserver = window.WebKitMutationObserver;
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log("Override existent new found top value with -> " + newHeight);
$(".select2-drop-active").css({'top':newHeight+"px"});
});
observer.disconnect();
console.log('pausing the observer');
// If needed for your use case, start observing in 1 second
setTimeout(() => observer.observe(), 1000);
});
关于javascript - MutationObserver 覆盖最新的突变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50386614/