Prevent Spamming of button to call function

html, jquery

Solution

You can lock (prevent the function logic from execution) the function in the following way:

var locked = false;
function alertStatus () {
    if (!locked) {
        locked = true;
        setTimeout(unlock, 1000);
        $('#doneStatus').hide();
        $('#loadingStatus').show();
    }
}

function unlock () {
    locked = false;
}

You can also disable the button such that the user cannot click it at all:

function alertStatus () {
    $('#idbuttonUpdateStatus').attr('disabled', 'disabled');
    setTimeout(enable, 1000);
    $('#doneStatus').hide();
    $('#loadingStatus').show();
}

function enable () {
    $('#idbuttonUpdateStatus').removeAttr('disabled');
}

Problem

How can I prevent Spamming of button on calling a function?? like the user can only call the function every 1 sec on the button. Is there a way doing it?? cause I tried setTimeout but it didnt works it still spamming BTW i used Jquery. here is my code: ``` <button class="buttonUpdateStatus" id="idbuttonUpdateStatus" onclick="alertStatus()">Post Status</button> function alertStatus() { $('#doneStatus').hide(); $('#loadingStatus').show(); } ```

Original source