admin管理员组文章数量:1201361
How can I disconnect my mutation observer from its callback function? The changes are being observed as they should, but I would like to disconnect the observer after the first change. Since the observer variable is out of scope, it's not disconnecting as it should. How can I pass the observer variable to the callback function so the code will work?
function mutate(mutations) {
mutations.forEach(function(mutation) {
if ( mutation.type === 'characterData' ) {
console.log('1st change.');
observer.disconnect(); // Should disconnect here but observer variable is not defined.
}
else if ( mutation.type === 'childList' ) {
console.log('2nd change. This should not trigger after being disconnected.');
}
});
}
jQuery(document).ready(function() {
setTimeout(function() {
document.querySelector('div#mainContainer p').innerHTML = 'Some other text.';
}, 2000);
setTimeout(function() {
jQuery('div#mainContainer').append('<div class="insertedDiv">New div!<//div>');
}, 4000);
var targetOne = document.querySelector('div#mainContainer');
var observer = new MutationObserver( mutate );
var config = { attributes: true, characterData: true, childList: true, subtree: true };
observer.observe(targetOne, config);
});
<script src=".1.1/jquery.min.js"></script>
<body>
<div id="mainContainer">
<h1>Heading</h1>
<p>Paragraph.</p>
</div>
</body>
How can I disconnect my mutation observer from its callback function? The changes are being observed as they should, but I would like to disconnect the observer after the first change. Since the observer variable is out of scope, it's not disconnecting as it should. How can I pass the observer variable to the callback function so the code will work?
function mutate(mutations) {
mutations.forEach(function(mutation) {
if ( mutation.type === 'characterData' ) {
console.log('1st change.');
observer.disconnect(); // Should disconnect here but observer variable is not defined.
}
else if ( mutation.type === 'childList' ) {
console.log('2nd change. This should not trigger after being disconnected.');
}
});
}
jQuery(document).ready(function() {
setTimeout(function() {
document.querySelector('div#mainContainer p').innerHTML = 'Some other text.';
}, 2000);
setTimeout(function() {
jQuery('div#mainContainer').append('<div class="insertedDiv">New div!<//div>');
}, 4000);
var targetOne = document.querySelector('div#mainContainer');
var observer = new MutationObserver( mutate );
var config = { attributes: true, characterData: true, childList: true, subtree: true };
observer.observe(targetOne, config);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div id="mainContainer">
<h1>Heading</h1>
<p>Paragraph.</p>
</div>
</body>
Share
Improve this question
asked Dec 25, 2016 at 19:24
GTS JoeGTS Joe
4,14217 gold badges61 silver badges108 bronze badges
1
- Does this answer your question? MutationObserver disconnect() call inside callback function – d4nyll Commented May 8, 2021 at 18:49
1 Answer
Reset to default 24The easiest way would be to adjust your callback
function mutate(mutations) {
to
function mutate(mutations, observer) {
because the observer instance associated with the mutations is automatically passed as the second parameter to the mutation handler function.
Then you can call observer.disconnect()
at whichever time you need it.
本文标签: javascriptDisconnect Mutation Observer from Callback FunctionStack Overflow
版权声明:本文标题:javascript - Disconnect Mutation Observer from Callback Function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738597442a2101866.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论