Add defer snippet
This commit is contained in:
21
snippets/defer.md
Normal file
21
snippets/defer.md
Normal file
@ -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
|
||||||
|
```
|
||||||
@ -22,6 +22,7 @@ countVowels:string
|
|||||||
currentURL:browser
|
currentURL:browser
|
||||||
curry:function
|
curry:function
|
||||||
deepFlatten:array
|
deepFlatten:array
|
||||||
|
defer:function
|
||||||
detectDeviceType:browser
|
detectDeviceType:browser
|
||||||
difference:array
|
difference:array
|
||||||
differenceWith:array
|
differenceWith:array
|
||||||
|
|||||||
Reference in New Issue
Block a user