Created
October 1, 2014 14:39
-
-
Save nblenke/45429953e719e45106ad to your computer and use it in GitHub Desktop.
domModified
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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