-
Notifications
You must be signed in to change notification settings - Fork 96
Open
Description
hi, I have a question to ask, in throttle function, I use es6's arrow function and spread operator(I remove arguments and func.apply), Is that right?
import 'babel-polyfill';
export const throttle = (func, wait) => {
let args, prevArgs, argsChanged, result;
let previous = 0;
return (...rest) => {
let now, remaining;
if(wait){
now = Date.now();
remaining = wait - (now - previous);
}
args = [...rest]; //use rest, remove arguments
argsChanged = JSON.stringify(args) != JSON.stringify(prevArgs);
prevArgs = Object.assign({}, args);
if (argsChanged || wait && (remaining <= 0 || remaining > wait)) {
if(wait){
previous = now;
}
result = func(...args); //and remove apply
args = null;
}
return result;
};
};Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels