自定义元素在删除后仍在运行

时间:2018-12-20 08:16:13

标签: javascript garbage-collection custom-element

我在标记中有一个自定义元素。在某些时候,我将整体替换为不同的内容/域,但是基于开发控制台,先前“ body”的自定义元素仍在运行(即获取某些数据)。删除/替换DOM时,是否应该将其垃圾回收(即JS exec停止)?   我是否需要使用connectedCallback手动处理删除操作(即停止进行中的提取请求等)?

代码样本(减少)

<body>
<x-search data-cursor="xyz"></x-search>
</body>

JS     //在实际代码中,这是另一个替换正文的自定义元素

function replacePage(newBody){
    // received an event (i.e. click on a navigation link) 
    // that requires body change
    //
    document.body = newBody.body
    // At this point I was expecting SearchElem to be garbage collected
    // (existing fetch requests aborted etc)
   // but I can see in the console that it's still running 
   // (i.e. fetching some items which perhaps started based
   // on an event received before the dom was removed).
}
class SearchElem extends HTMLElement {
    constructor() {
        super(); 
        this.SearchPull = {
            "top": 200,
            "down": 200,
        };
        // Listen for various events such 'focus'
        // on each event scan the DOM(#search-results) and fetch 
        // new results or remove from existing top/tail if necessary.
        this.ListenEvents()
        let wrapper = this.querySelector("#search-results");
        this.searchPolicy = new SearchPolicy(this.SearchPull, wrapper, cursorHistory);
    }
}

0 个答案:

没有答案