Last active
December 22, 2015 23:19
-
-
Save reu/6546038 to your computer and use it in GitHub Desktop.
Returns a function that as long as it continues to be called, it will not be invoked. It will only be called after `time` milliseconds.
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
function debounce(fn, time) { | |
var time = time || 300, | |
scope = this, | |
delay; | |
return function() { | |
var args = arguments; | |
if (delay) clearTimeout(delay); | |
delay = setTimeout(function() { | |
fn.apply(scope, args); | |
}, time); | |
} | |
} |
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
$("input").on("keyup", debounce(function() { | |
$.get("/search", { query: $(this).val() }, updateSearchResults); | |
})); |
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 callTimes = 0; | |
function fn() { | |
callTimes += 1; | |
if (callTimes > 1) console.assert(false, "Expected only one call, but was called "+ callTimes +" times"); | |
} | |
var debouncedFn = debounce(fn, 0); | |
debouncedFn(); | |
debouncedFn(); | |
debouncedFn(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment