From c8ac1c263ba8d027866a51afeb3667c14ca76eff Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 2 Jan 2018 08:40:31 +1100 Subject: [PATCH 1/3] Add defer snippet --- snippets/defer.md | 21 +++++++++++++++++++++ tag_database | 1 + 2 files changed, 22 insertions(+) create mode 100644 snippets/defer.md diff --git a/snippets/defer.md b/snippets/defer.md new file mode 100644 index 000000000..88706b86d --- /dev/null +++ b/snippets/defer.md @@ -0,0 +1,21 @@ +### defer + +Defers invoking a function until the current call stack has cleared. + +Use `window.setTimeout()` with a timeout of 1ms to add a new event to the browser +event queue and allow the rendering engine to complete its work. Use the spread/rest (`...`) +operator to supply the function with an arbitrary number of arguments. + +```js +const defer = (fn, ...args) => setTimeout(fn, 1, ...args); +``` + +```js +// Example A: +defer(console.log, 'a'), console.log('b'); // logs 'b' then 'a' + +// Example B: +document.querySelector('#someElement').innerHTML = 'Hello'; +longRunningFunction(); // The browser will not update the HTML until this has finished +defer(longRunningFunction); // The browser will update the HTML then run the function +``` diff --git a/tag_database b/tag_database index 708f71b32..13d2ab730 100644 --- a/tag_database +++ b/tag_database @@ -22,6 +22,7 @@ countVowels:string currentURL:browser curry:function deepFlatten:array +defer:function detectDeviceType:browser difference:array differenceWith:array From b2c5630a12646f1bbc85314413ab721c49179923 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 2 Jan 2018 09:47:52 +1100 Subject: [PATCH 2/3] Update defer.md --- snippets/defer.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/defer.md b/snippets/defer.md index 88706b86d..1a0a63649 100644 --- a/snippets/defer.md +++ b/snippets/defer.md @@ -2,7 +2,7 @@ Defers invoking a function until the current call stack has cleared. -Use `window.setTimeout()` with a timeout of 1ms to add a new event to the browser +Use `setTimeout()` with a timeout of 1ms to add a new event to the browser event queue and allow the rendering engine to complete its work. Use the spread/rest (`...`) operator to supply the function with an arbitrary number of arguments. @@ -16,6 +16,6 @@ defer(console.log, 'a'), console.log('b'); // logs 'b' then 'a' // Example B: document.querySelector('#someElement').innerHTML = 'Hello'; -longRunningFunction(); // The browser will not update the HTML until this has finished -defer(longRunningFunction); // The browser will update the HTML then run the function +longRunningFunction(); // the browser will not update the HTML until this has finished +defer(longRunningFunction); // the browser will update the HTML then run the function ``` From 86f3f7055ed31b12404c7e0d84df0170d579d8da Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 2 Jan 2018 10:39:19 +0200 Subject: [PATCH 3/3] Update defer.md --- snippets/defer.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/snippets/defer.md b/snippets/defer.md index 1a0a63649..7403d4c36 100644 --- a/snippets/defer.md +++ b/snippets/defer.md @@ -2,9 +2,7 @@ Defers invoking a function until the current call stack has cleared. -Use `setTimeout()` with a timeout of 1ms to add a new event to the browser -event queue and allow the rendering engine to complete its work. Use the spread/rest (`...`) -operator to supply the function with an arbitrary number of arguments. +Use `setTimeout()` with a timeout of 1ms to add a new event to the browser event queue and allow the rendering engine to complete its work. Use the spread (`...`) operator to supply the function with an arbitrary number of arguments. ```js const defer = (fn, ...args) => setTimeout(fn, 1, ...args);