防止按钮的垃圾邮件调用功能 [英] Prevent Spamming of button to call function

查看:99
本文介绍了防止按钮的垃圾邮件调用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在调用函数时阻止按钮的垃圾邮件?比如用户只能在按钮上每1秒调用一次该功能。

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.

有没有办法做到这一点?因为我尝试了setTimeout但它没有工作它仍然垃圾邮件
BTW我使用Jquery。

Is there a way doing it?? cause I tried setTimeout but it didnt works it still spamming BTW i used Jquery.

这是我的代码:

<button class="buttonUpdateStatus" id="idbuttonUpdateStatus" onclick="alertStatus()">Post Status</button>



 function alertStatus() {
        $('#doneStatus').hide();
        $('#loadingStatus').show();
  }


推荐答案

你可以锁定(阻止来自执行的函数逻辑)函数按以下方式:

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');
}

这篇关于防止按钮的垃圾邮件调用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆