From fac45a51002bd54b3c17504c78a35ccd55320df1 Mon Sep 17 00:00:00 2001 From: atomiks Date: Mon, 12 Feb 2018 16:58:15 +1000 Subject: [PATCH 1/2] Simplify debounce.md --- snippets/debounce.md | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/snippets/debounce.md b/snippets/debounce.md index 941ab59a3..71fdd9c40 100644 --- a/snippets/debounce.md +++ b/snippets/debounce.md @@ -1,19 +1,16 @@ ### debounce -Creates a debounced function that delays invoking the provided function until after `wait` milliseconds have elapsed since the last time the debounced function was invoked. +Creates a debounced function that delays invoking the provided function until at least `ms` milliseconds have elapsed since the last time it was invoked. -Use `setTimeout()` and `clearTimeout()` to debounce the given method, `fn`. -Use `Function.apply()` to apply the `this` context to the function and provide the necessary `arguments`. -Omit the second argument, `wait`, to set the timeout at a default of 0 ms. +Each time the supplied function is invoked, clear the current pending timeout with `clearTimeout()` and use `setTimeout()` to create a new timeout that delays invoking the function until at least `ms` milliseconds has elapsed. Use `Function.apply()` to apply the `this` context to the function and provide the necessary arguments. +Omit the second argument, `ms`, to set the timeout at a default of 0 ms. ```js -const debounce = (fn, wait = 0) => { - let inDebounce; - return function() { - const context = this, - args = arguments; - clearTimeout(inDebounce); - inDebounce = setTimeout(() => fn.apply(context, args), wait); +const debounce = (fn, ms = 0) => { + let timeoutId; + return function(...args) { + clearTimeout(timeoutId); + timeoutId = setTimeout(() => fn.apply(this, args), ms); }; }; ``` @@ -21,7 +18,7 @@ const debounce = (fn, wait = 0) => { ```js window.addEventListener( 'resize', - debounce(function(evt) { + debounce(() => { console.log(window.innerWidth); console.log(window.innerHeight); }, 250) From a0f8ad615ce26b73789cd9d40bf68c33d6396e42 Mon Sep 17 00:00:00 2001 From: atomiks Date: Mon, 12 Feb 2018 16:59:30 +1000 Subject: [PATCH 2/2] Update debounce.md --- snippets/debounce.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/debounce.md b/snippets/debounce.md index 71fdd9c40..6724c7d15 100644 --- a/snippets/debounce.md +++ b/snippets/debounce.md @@ -2,7 +2,7 @@ Creates a debounced function that delays invoking the provided function until at least `ms` milliseconds have elapsed since the last time it was invoked. -Each time the supplied function is invoked, clear the current pending timeout with `clearTimeout()` and use `setTimeout()` to create a new timeout that delays invoking the function until at least `ms` milliseconds has elapsed. Use `Function.apply()` to apply the `this` context to the function and provide the necessary arguments. +Each time the debounced function is invoked, clear the current pending timeout with `clearTimeout()` and use `setTimeout()` to create a new timeout that delays invoking the function until at least `ms` milliseconds has elapsed. Use `Function.apply()` to apply the `this` context to the function and provide the necessary arguments. Omit the second argument, `ms`, to set the timeout at a default of 0 ms. ```js