JSFiddle - React, Tailwind, and code Playground

by adeneo

HTML

<button id="myfunction">Alert hello</button>
<button id="mystopfunction">Stop before 3 seconds</button>

JavaScript

$(document).ready(function(){
    var myVar = [];
    
    $("#myfunction").click(myFunction);
    $("#mystopfunction").click(myStopFunction);
    
    function myFunction() {
        for(i=0;i<10;i++){
            myVar.push(
                setTimeout(function(){
                    alert("Hello")
                }, 3000)
            );
        }
    }
    
    function myStopFunction() {
        myVar.forEach(function(timer) {
            clearTimeout(timer);
        });
    }
});