Last active
January 13, 2016 19:22
-
-
Save yckart/6140932 to your computer and use it in GitHub Desktop.
throttle/debounce javascript
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
// call after last delay | |
function debounce(callback, delay) { | |
var timeout; | |
return function () { | |
var self = this, args = arguments; | |
if (timeout) clearTimeout(timeout); | |
timeout = setTimeout(function () { | |
callback.apply(self, args); | |
}, delay || 100); | |
}; | |
} |
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
module.exports = { | |
throttle: function (callback, delay) { | |
var timeout; | |
return function () { | |
if (timeout) return; | |
timeout = setTimeout(function () { | |
timeout = false; | |
}, delay || 100); | |
callback.apply(this, arguments); | |
}; | |
}, | |
debounce: function (callback, delay) { | |
var timeout, self, args; | |
return function () { | |
self = this; | |
args = arguments; | |
if (timeout) clearTimeout(timeout); | |
timeout = setTimeout(function () { | |
callback.apply(self, args); | |
}, delay || 100); | |
}; | |
} | |
} |
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
// call before each delay | |
function throttle(callback, delay) { | |
var timeout; | |
return function () { | |
if (timeout) return; | |
timeout = setTimeout(function () { | |
timeout = false; | |
}, delay || 100); | |
callback.apply(this, arguments); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment