简体   繁体   English

如何使点击事件在几秒钟内对用户不可点击?

[英]How to make click event non-clickable for user for a few seconds?

How to make click event non-clickable for user for a few seconds once the user has clicked the button? 一旦用户单击按钮,如何使用户单击事件几秒钟无法点击?

For eg. 例如。

$('button').on('click',function(){
alert('you clicked');
}

It alerts every time. 每次都发出警报。 But I want to make that button something else whatever that I've assigned the click event on, here in example, if the user clicks again within next 20 seconds it should not work and after 20 seconds the button should be clickable that is if the user clicks after 20 seconds it alerts again. 但是我想使该按钮具有其他功能,无论我在其上分配了click事件如何,例如,如果用户在接下来的20秒内再次单击它,则该按钮将不起作用,并且20秒后,该按钮应可单击,即用户点击20秒后再次发出警报。

 $('button').on('click',function(){
  alert('you clicked');
  $(this).prop("disabled", true);
  setTimeout( function(){$(this).prop("disabled", false);},20000 );
 }

Not tested but : 未经测试,但:

var isClickable = true;
$('button').on('click',function(){
    //Exit if not clickable
    if(!isClickable)
        return false;

    //Set not clickable
    isClickable = false;
    //And finally plan to reset clickable in 20 seconds 
    setTimeout(function(){isClickable=true;},20000);

    //Then, your code
    alert('you have just clicked');
}

http://jsfiddle.net/bzY62/ http://jsfiddle.net/bzY62/

$('button').on('click', function(){
    var _$this = $(this);

    if(!_$this.data('blocked')) {
        alert('you clicked');

        _$this.data('blocked', true);

        setTimeout(function() {
           _$this.data('blocked', false);
        }, 5000); // in miliseconds
    }
});

You can try this 你可以试试这个

var seconds = 5; //seconds to disable a button
$('#yourbutton').click(function(){ //yourbutton is the id of button
    var btn = $(this);
    btn.prop('disabled', true);  //disables the buttons
    setTimeout(function(){  //renable the button after seconds specified
        btn.prop('disabled', false);
    }, seconds*1000);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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