Last active
August 29, 2015 14:00
-
-
Save matteodelabre/11098809 to your computer and use it in GitHub Desktop.
AMD module that enables to debounce functions, i.e to limit them to be called once every X seconds
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
/*globals define */ | |
define(function () { | |
'use strict'; | |
/** | |
* debounce module | |
* This module is freely usable, without any license | |
* Based on John Resig's work | |
* | |
* @param {function} [func] - Function to be debounced | |
* @param {int} [treshold=100] - Time limit | |
**/ | |
return function (func, threshold) { | |
var timeout; | |
return function debounced() { | |
var obj = this, args = arguments; | |
function delayed() { | |
func.apply(obj, args); | |
timeout = null; | |
} | |
if (timeout) { | |
clearTimeout(timeout); | |
} | |
timeout = setTimeout(delayed, threshold || 100); | |
}; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment