LoginSignup
1
2

More than 5 years have passed since last update.

ボタンの連打を防ぐ

Posted at

ボタンを押された時にボタンを無効化し、タイマーで一定時間後に有効化すればよいだけなのだが
setTimeoutでコールされるfunctionでなんとなくthisを操作しても当然動かず、、、

きちんとタグの受け渡しを行いましょう


val timer_button_tag;
$('#reload').click(function(){
    $(this).prop("disabled", true);

    //タイマー内のfunctionへthisを引き継げないため変数に保持することで押されたボタンを操作
    timer_button_tag = this;
    setTimeout(function(){
        $(timer_button_tag).prop("disabled", true);    //保持した変数でボタンを操作。thisでやりがち
    },1000);

});
1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2