Created
May 29, 2013 15:05
-
-
Save hikarock/5671000 to your computer and use it in GitHub Desktop.
setTimeoutでdebounce
This file contains hidden or 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
* { | |
margin: 0; | |
padding: 0; | |
border: 0; | |
} | |
body { | |
background: #ffd; | |
font: 30px sans-serif; | |
} |
This file contains hidden or 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
<button>save</button> |
This file contains hidden or 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
var _timerId = null; | |
function save() { | |
var delay = 10 * 1000; | |
// 実行待ち状態でもう一度呼ばれたら予約してある処理をキャンセル | |
if (_timerId) { | |
console.log("cancel"); | |
clearTimeout(_timerId); | |
} | |
_timerId = setTimeout(function () { | |
console.log("wait end"); | |
_timerId = null; | |
// 超重い保存処理 | |
}, delay); | |
} | |
function handler() { | |
console.log("wait start"); | |
save(); | |
} | |
$("button").on("click", handler); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment