我应该如何将 debounce 与 MutationObserver 结合起来

dar*_*bid 7 javascript mutation-observers debouncing

我想知道突变何时停止被调用。因此我正在实施去抖。然而我的 console.log('*****Debounced ****'); 从未被调用过。

您能帮助我了解我做错了什么吗?

var target = document.body;
var observer = new MutationObserver(function(mutations) 
{
    mutations.forEach(function(mutation) 
        {
            for (var i = 0; i < mutation.addedNodes.length; i++) 
                {
                    var item = mutation.addedNodes[i];
                    console.log(item);
                    myCustomFunction
                }
        });
});         

observer.observe(document, {childList: true, subtree: true});

var myCustomFunction = debounce(function() 
               {
                   console.log('*****Debounced ****');
                   //call some method do more work etc
                }, 500);

function debounce(func, wait, immediate) 
{
var timeout;
return function() {
    var context = this,
        args = arguments;
    var later = function() {
        timeout = null;
        if ( !immediate ) {
            func.apply(context, args);
        }
    };
    var callNow = immediate && !timeout;
    clearTimeout(timeout);
    timeout = setTimeout(later, wait || 1000);
    if ( callNow ) {
        func.apply(context, args);
    }
};
}
Run Code Online (Sandbox Code Playgroud)