Last active
February 18, 2016 09:34
-
-
Save nekoneko-wanwan/65dfe6fb29260d8d3070 to your computer and use it in GitHub Desktop.
二重クリックの防止
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 追記 | |
// 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を切り替えると良いかと |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
これは単純に2000ミリ秒後に実行できるようにしているだけなので、ずっとクリックしていても時間が過ぎればそのまま処理される
clearTimeout()やnew Date()を使って連続処理の間引きを行うようにすると、連続クリックしても必ず1回のみ処理されるようになる。ただしその場合、禁止中に別途処理(文言を変えたりdisabledにしたり)を行うのが手間そう