Create simple debounce and throttle instead of _.

This commit is contained in:
Hongarc 2018-12-05 12:08:41 +07:00
parent ae0030daa7
commit e6d7edd130
7 changed files with 43 additions and 8 deletions

17
src/js/util/debounce.js Normal file
View file

@ -0,0 +1,17 @@
module.exports = function(func, time, immediate) {
var timeout;
return function() {
var later = function() {
timeout = null;
if (!immediate) {
func.apply(this, arguments);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, time);
if (callNow) {
func.apply(this, arguments);
}
};
};

13
src/js/util/throttle.js Normal file
View file

@ -0,0 +1,13 @@
module.exports = function(func, time) {
var wait = false;
return function() {
if (!wait) {
func.apply(this, arguments);
wait = true;
setTimeout(function() {
wait = false;
}, time);
}
};
};