Been looking for a debounce function or way to debounce in Jquery. The build up of animations can get super annoying. Heres the code:
function fade() {
$(\
I would just include underscore.js in my project and use the debounce function that it contains. It works great. I've used it in multiple projects.
http://underscorejs.org/#debounce
debounce_.debounce(function, wait, [immediate]) Creates and returns a new debounced version of the passed function which will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked. Useful for implementing behavior that should only happen after the input has stopped arriving. For example: rendering a preview of a Markdown comment, recalculating a layout after the window has stopped being resized, and so on.
At the end of the wait interval, the function will be called with the arguments that were passed most recently to the debounced function.
Pass true for the immediate argument to cause debounce to trigger the function on the leading instead of the trailing edge of the wait interval. Useful in circumstances like preventing accidental double-clicks on a "submit" button from firing a second time.
var lazyLayout = _.debounce(calculateLayout, 300); $(window).resize(lazyLayout);
I don´t like the idea to include a library just for a debounce function. You can just do:
var debounce = null;
$('#input').on('keyup', function(e){
clearTimeout(debounce );
debounce = setTimeout(function(){
$.ajax({url: 'someurl.jsp', data: {query: q}, type: 'GET'})
}, 100);
});