Prevent JavaScript function from running twice (setTimeout)

后端 未结 4 1002
一整个雨季
一整个雨季 2021-01-12 13:10

I have this function that runs for several seconds with the use of setTimeout. This function is ran when a button is clicked.

function complete(         


        
4条回答
  •  我在风中等你
    2021-01-12 13:53

    Assuming you've used addEventListener to attach the click event to the button, you could remove the listener while timeouts are in progress:

    function complete() {
        var div = document.getElementById('log'),
            button = this;
        this.removeEventListener('click', complete);
        setTimeout(function(){...});
                     :
        setTimeout(function(){...});
        // If you later want to make the button clickable again, just uncomment the line below:
        // setTimeout(function () {button.addEventListener('click', complete);}, 10000);
    }
    

提交回复
热议问题