-
-
Save tongxunlu/7494591 to your computer and use it in GitHub Desktop.
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
| // execute callback only after a pause in user input; the function returned | |
| // can be used to handle an event type that tightly repeats (such as typing | |
| // or scrolling events); it will execute the callback only if the given timout | |
| // period has passed since the last time the same event fired | |
| function createOnPause(callback, timeout, _this) { | |
| return function(e) { | |
| var _that = this; | |
| if (arguments.callee.timer) | |
| clearTimeout(arguments.callee.timer); | |
| arguments.callee.timer = setTimeout(function() { | |
| callback.call(_this || _that, e); | |
| }, timeout); | |
| } | |
| } | |
| // Use it like this: | |
| document.addEventListener('scroll', createOnPause(function(e) { | |
| // do something interesting here | |
| }, 1500, this), false); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
事件延迟运行