Created
September 16, 2014 12:30
-
-
Save kanakiyajay/0a677c5b48ee4cf8a361 to your computer and use it in GitHub Desktop.
Postpone/Debounce a function important for showing autocomplete search results and saving ajax requests.
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 debounce (delay, callback) { | |
var timeout = null; | |
return function (){ | |
if (timeout) { | |
clearTimeout(timeout); | |
} | |
var context = this; | |
var args = arguments; | |
timeout = setTimeout(function(){ | |
callback.apply(context, args); | |
timeout = null; | |
}, delay) | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment