Throttle a Function
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
Write throttle(fn, wait): return a function that calls fn at most once per wait milliseconds. A call when no wait is running calls fn straight away with its arguments and starts a wait. Calls during a wait don't call fn, but the latest one's arguments are kept: when the wait ends, if there were any, fn is called once with them and a new wait starts. If there weren't, nothing happens until the next call. The returned function returns undefined, and the checks use waits of 50 to 60 milliseconds.
Your code must pass
- Calls fn straight away on the first call
- Makes one trailing call with the latest arguments
- Doesn't make a trailing call when nothing came in
- Calls at most once per wait under steady calls