Skip to content

Instantly share code, notes, and snippets.

@ssmylh
Created March 8, 2015 01:54
Show Gist options
  • Select an option

  • Save ssmylh/a63c290c542a57e0f143 to your computer and use it in GitHub Desktop.

Select an option

Save ssmylh/a63c290c542a57e0f143 to your computer and use it in GitHub Desktop.
Mutation Observer
<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>MutationObserver Sample</title>
<script>
/* https://developer.mozilla.org/ja/docs/Web/API/MutationObserver */
document.addEventListener('DOMContentLoaded', start);
function start() {
var mo = new MutationObserver(function(mutations, observer) {
mutations.forEach(function(mutation) {
console.log('set display none.')
mutation.target.style.display = 'none';
});
// Clear this MutationObserver instance's record queue,
// in order to prevent this callback from being called infinitely.
observer.takeRecords();
});
var target = document.getElementById('target');
var config = { attributes: true };
mo.observe(target, config);
setInterval(function() {
console.log('set display block.');
target.style.display = '';
}, 5000);
}
</script>
</head>
<body>
<span id="target" style="display:none">text</span>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment