简体   繁体   中英

Remove javascript code using jQuery

I used

<div id="myJSDiv"><script>...</script></div>
$("#myJSDiv").html('');

and

<script id="myJSDiv">...</script>
$("#myJSDiv").remove();

but nothing removed.

JS script is

<script id="count_down">
        var ss = 150;
        function countdown() {
            ss = ss-1;
            if (ss<0) {
                window.location="google.com";
            }else {
                document.getElementById("countdown").innerHTML=ss;
                window.setTimeout("countdown()", 1000);
            }
        }
        </script>

Once a script has loaded, removing the script tag it came from does not do anything to the functions or variables defined or created by that script.

There is no way to remove a block of code like that after it has loaded. Global functions can be redefined after they were loaded, but that's about all you can do. Perhaps, if you described the overall problem you are trying to solve, we could suggest a better approach.


Now that you've added the actual problem to your question, to stop your countdown timer, you can do this:

 <script id="count_down">
    var ss = 150;
    var countdownTimer;
    function countdown() {
        ss = ss-1;
        if (ss<0) {
            window.location="google.com";
        }else {
            document.getElementById("countdown").innerHTML=ss;
            countdownTimer = window.setTimeout(countdown, 1000);
        }
    }

    function stopCountdown() {
        clearTimeout(countdownTimer);
    }
    </script>

Just call the stopCountdown() function when you want to stop the countdown timer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM