Unload javascript file when modal dialog is closed

30
January 14, 2022, at 02:40 AM

I have a modal dialog that has been styled to show along the right hand side of the customers browser. Depending on the icon they click in a nav bar (here is an example) ...

<a class="nav-link right-menu" role="button" data-bs-toggle="modal" data-bs-target="#rightSubMenu" data-id="admin-menu">
    <i class="fas fa-users-crown"></i>
</a>

An ajax call is made and an HTML fragment is loaded in to the modal-body via an additional class named .right-sub-menu-content. Here is the ajax call ...

$('.right-menu').click( function() {
    var requestedMenu = $(this).attr('data-id');
    $.ajax({
        type: "POST",
        url: "//" + $(location).attr('hostname') + "/modules/",
        data: {
            'module': requestedMenu
        },
        dataType: "html",
        cache: false,
        success: function(data) {
            $('.right-sub-menu-content').html(data);
        }
    });
});

I am also loading a js file to handle buttons, additional select menu updates, etc. Its loaded using a standard <script src="something.js"></script> tag as part of the HTML fragment.

My problem -- In one particular fragment I load a dashboard to provide my admins server status updates. It refreshes several DIVs once every 5 minutes via ajax by using a setInterval.

I found out that after closing the modal dialog, that setInterval continues to run, even though I destroy the contents of the modal-body, which I thought would include the script that was loaded as part of the fragment, using the following in the main.js file ...

$('.modal.right').on('hidden.bs.modal', function() {
    $('.right-sub-menu-content').html('');
});

So my question is ... since I'm reusing the modal dialog for other HTML fragments which include their own JS files, how can I unload the js file completely when the modal is closed?

Thank you all for any insight.

Answer 1

I think you should stop the setInterval function with clearInterval maybe this will work for you

READ ALSO
Node server :remote-addr displayed local IP (192.X.X.X) when accessed from python-requests

Node server :remote-addr displayed local IP (192.X.X.X) when accessed from python-requests

I have an express server that uses nginx and monitors the X-Forwarded-For header

42
MySQL - How to get a list of Session Variables and their Values?

MySQL - How to get a list of Session Variables and their Values?

Does anyone know if we can get a list of custom session variables for the current session in the latest version of MySQL?

31
How do I include other files in PHP?

How do I include other files in PHP?

I just started creating a website at my home

56
What causes CSS, HTML, javascript, or React to combine margins?

What causes CSS, HTML, javascript, or React to combine margins?

I have an element with a margin at the bottom of 10pxAnd immediately under this element, I place another element with margin at the top of 10px

44