Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
How to remove library from memory in JavaScript?

There are multi libraries that each of them generates some HTML elements which runs separately such that by running one library first the elements created by the previous library must be removed then new elements will generated. It's worked fine, the elements removed correctly and new ones created correctly.

But there is a problem : the previous library that runs, again exist in the memory and listen to events. How can I dispose that from memory ?

import { LeftSidebarEditor } from './editor/LeftSidebar.Editor.js';
import { LeftSidebarSupport } from './support/LeftSidebar.Support.js';

function LeftSidebar(editor) {

    var signals = editor.signals;

    var container = new UIPanel();
    container.setId('left-sidebar-menu');

    var item = new LeftSidebarEditor(editor);

    container.add(item);

    signals.scopeChanged.add(function (scope) {

        editor.currentScope = JSON.stringify(scope);
        // remove all elements
        container.clear();

        item = null;

        switch (scope) {

             case 'editor':
                // container.add(new LeftSidebarEditor(editor));
                item = new LeftSidebarEditor(editor);

                container.add(item);
                break;

            case 'support':
                // container.add(new LeftSidebarSupport(editor));
                item = new LeftSidebarSupport(editor);

                container.add(item);
                break;

        }

    });

    return container;

}

LeftSidebarSupport

class LeftSidebarSupport {

    constructor(editor) {

        let counter = 0;
        setInterval(() => {
            console.log('I am alive from support', counter++);
        }, 2000);

         signals.objectSelected.add(function (object) {

        if (object != null) {

            return;
        }

        const selected = document.querySelector('#left-sidebar-menu li.selected');
        if (selected){
            let selected_attr =  selected.getAttribute('data-tooltip');
            if (selected_attr!='Tag'){
                removeAllSelected();
            }
        }

    });


        function removeAllSelected() {

        const selected = document.querySelector('#left-sidebar-menu li.selected');

        if (selected) {

            selected.classList.remove('selected');

        }

    }

    }
}

By change the signal from support to editor again the logs printed which must be stopped !! I want by switching between scopes just exist current scope library in memory and others removed entirely.

I am alive from support 89
I am alive from support 90
I am alive from support 91
I am alive from support 92
I am alive from support 93

Pleas, note !

There really is no interval, but exist a piece of code that removes a class from the elements. For simplicity, the interval example is used here. The basis of my question is why can LeftSidebarSupport running again?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!