Skip to content

Instantly share code, notes, and snippets.

@nblenke
Created October 1, 2014 14:39
Show Gist options
  • Save nblenke/45429953e719e45106ad to your computer and use it in GitHub Desktop.
Save nblenke/45429953e719e45106ad to your computer and use it in GitHub Desktop.
domModified
/*
domModified
Add mutation listener to DOM element and trigger callback once it has completed
https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
Usage:
domModified({
selector: '#selector',
callback: function (ev) {}
}
*/
var domModified = function (args) {
var target = document.querySelector(args.selector),
timer = 0,
observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
clearTimeout(timer);
timer = setTimeout(function () {
args.callback(mutation);
observer.disconnect();
}, 500);
});
}),
config = {
attributes: true,
childList: true,
characterData: true
};
if (target) {
observer.observe(target, config);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment