6

I would like to know when the mutations have stopped being called. I am therefore implementing a debounce. However my console.log('*****Debounced ****'); is never called.

Could you please help me to understand what I am doing wrong.

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);
    }
};
}
darbid
  • 2,545
  • 23
  • 55
  • 2
    `myCustomFunction` statement is just a reference. To invoke it, add `()` right after it. – wOxxOm Jan 03 '19 at 06:56
  • @wOxxOm Thank you. I have been working too long. I think it is time for a break. Thank you again. – darbid Jan 03 '19 at 06:59

0 Answers0