Skip to content

Instantly share code, notes, and snippets.

@nekoneko-wanwan
Last active February 18, 2016 09:34
Show Gist options
  • Save nekoneko-wanwan/65dfe6fb29260d8d3070 to your computer and use it in GitHub Desktop.
Save nekoneko-wanwan/65dfe6fb29260d8d3070 to your computer and use it in GitHub Desktop.
二重クリックの防止
// 追記
// functionは外に出して、callback付きで制御した方が良さそう
var isClickable = true;
$('h1').on('click',function() {
if (isClickable) {
console.log('実行!');
isClickable = false;
} else {
console.log('処理中です');
return false;
}
var that = this;
setTimeout(function() {
isClickable = true;
}, 2000);
});
// inputなどに適用する場合は、disabled: true / falseを切り替えると良いかと
@nekoneko-wanwan
Copy link
Author

これは単純に2000ミリ秒後に実行できるようにしているだけなので、ずっとクリックしていても時間が過ぎればそのまま処理される

clearTimeout()やnew Date()を使って連続処理の間引きを行うようにすると、連続クリックしても必ず1回のみ処理されるようになる。ただしその場合、禁止中に別途処理(文言を変えたりdisabledにしたり)を行うのが手間そう

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment