6. implement basic debounce()

easy  - accepted / - tried

Debounce is a common technique used in Web Application, in most cases using lodash solution would be a good choice.

could you implement your own version of basic debounce()?

In case you forgot, debounce(func, delay) will returned a debounced function, which delays the invoke.

Here is an example.

Before debouncing we have a series of calling like

─ A ─ B ─ C ─ ─ D ─ ─ ─ ─ ─ ─ E ─ ─ F ─ G 

After debouncing at wait time of 3 dashes

─ ─ ─ ─ ─ ─ ─ ─ D ─ ─ ─ ─ ─ ─ ─ ─ ─ G 

notes

  1. please follow above spec. the behavior might not be exactly the same as lodash.debounce()

  2. because window.setTimeout and window.clearTimeout are not accurate in browser environment, they are replaced to other implementation when judging your code. They still have the same interface, and internally keep track of the timing for testing purpose.

Something like below will be used to do the test.

let currentTime = 0const run = (input) => {  currentTime = 0  const calls = []  const func = (arg) => {     calls.push(`${arg}@${currentTime}`)  }  const debounced = debounce(func, 3)  input.forEach((call) => {     const [arg, time] = call.split('@')     setTimeout(() => debounced(arg), time)  })  return calls}expect(run(['A@0', 'B@2', 'C@3'])).toEqual(['C@5'])

Always try to find a better approach.