如何从另一个函数内部断开mutationObserver?

时间:2018-04-02 23:36:18

标签: javascript

        function observeMutations(object){
        // Select the node that will be observed for mutations

        var targetNode = object
        console.log(targetNode)

        // Options for the observer (which mutations to observe)
        var config = { attributes: true, childList: true };

        // Callback function to execute when mutations are observed
        var callback = function(mutationsList) {
            console.log(mutationsList)
            for(var mutation of mutationsList) {
                if (mutation.type == 'childList') {
                    console.log(mutation)
                    console.log('A child node has been added or removed.');
                }
                else if (mutation.type == 'attributes') {
                    console.log('The ' + mutation.attributeName + ' attribute was modified.');
                }
            }
        };

        // Create an observer instance linked to the callback function
        var observer = new MutationObserver(callback);

        // Start observing the target node for configured mutations
        observer.observe(targetNode, config);

    }

我在其他函数内的dom元素上运行此函数,问题是当我执行函数时它在同一个dom元素上运行多次。如何断开连接或不在每个dom元素中多次运行它?

我想从函数外部禁用它,而不是从内部禁用它。

0 个答案:

没有答案