Skip to content

could throttle function remove "arguments and apply method"? #29

@lynxerzhang

Description

@lynxerzhang

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;
  };
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions