From d50f7e3fb0210cc88e992dc56bc13c4c1b2f686a Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Mon, 12 Feb 2018 07:22:00 +0000 Subject: [PATCH] Travis build: 1649 --- README.md | 21 +++++++++------------ docs/index.html | 14 ++++++-------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index d6f750d77..7083b13b8 100644 --- a/README.md +++ b/README.md @@ -3822,20 +3822,17 @@ curry(Math.min, 3)(10)(50)(2); // 2 ### 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 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 -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); }; }; ``` @@ -3846,7 +3843,7 @@ const debounce = (fn, wait = 0) => { ```js window.addEventListener( 'resize', - debounce(function(evt) { + debounce(() => { console.log(window.innerWidth); console.log(window.innerHeight); }, 250) diff --git a/docs/index.html b/docs/index.html index 110dec1cb..385f479a6 100644 --- a/docs/index.html +++ b/docs/index.html @@ -857,18 +857,16 @@ console.log< arity <= args.length ? fn(...args) : curry.bind(null, fn, arity, ...args);
curry(Math.pow)(2)(10); // 1024
 curry(Math.min, 3)(10)(50)(2); // 2
-

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.

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.

const debounce = (fn, wait = 0) => {
-  let inDebounce;
-  return function() {
-    const context = this,
-      args = arguments;
-    clearTimeout(inDebounce);
-    inDebounce = setTimeout(() => fn.apply(context, args), wait);
+

debounce

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 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.

const debounce = (fn, ms = 0) => {
+  let timeoutId;
+  return function(...args) {
+    clearTimeout(timeoutId);
+    timeoutId = setTimeout(() => fn.apply(this, args), ms);
   };
 };
 
window.addEventListener(
   'resize',
-  debounce(function(evt) {
+  debounce(() => {
     console.log(window.innerWidth);
     console.log(window.innerHeight);
   }, 250)