sindresorhus / debounce

Delay function calls until a set time elapses after the last invocation
Other
798 stars 80 forks source link

Run debounce() callback if browser tab is closed #38

Closed h closed 1 year ago

h commented 1 year ago

This is a very common problem with debounced API calls: when the user closes the browser tab, the debounced function may not have run yet. This is a frequent cause of data loss. The _.debounce() function should avoid this issue by running the trailing debounce immediately if the tab is closed prior to the trailing timeout. This should only happen while running inside a browser because it's not relevant in headless runtimes (e.g. Node.js).

sindresorhus commented 1 year ago

https://github.com/sindresorhus/debounce/pull/39#issuecomment-1813110701