@debounce
Anti-shake function: When you call the function n seconds, the action will be performed once and re-calculate the execution time ago that if n seconds in turn calls the function will be canceled.
Use Case
1 |
import { debounce } from 'core-decorators'; |
1 |
<textarea id="editor"></textarea> |
For example, when listening id is editor editor input, each entry will trigger oninput
an event, call the this.updateContent
method. In order to prevent frequent content updates, can updateContent
decorative debounce method, not to update during the input this.content
, the input is completed until 500 msec, again a one-time content updates content.
Source
1 |
import { metaFor } from './private/utils'; |
Call parameters
Support incoming call when the wait and options.immediate two parameters:
- wait: time awaiting execution unit ms, default value 300
- {Immediate}: whether to perform the binding function, the default value is false, waiting time before execution wait; performed first parameter passed to true
Meta Object
1 |
const {} = debounceTimeoutIds metaFor ( the this ); |
metaFor
A method to increase the current instance Symbol('__core_decorators__')
attribute, a value Meta object.
Meta type as shown below, including attributes used debounce and throttle:
1 |
class Meta { |
使用了@lazyInitialize
标记,可以使得每次只初始化被用到的属性,像debounce的时候只用到debounceTimeoutIds属性。console输出Meta对象,结构如图所示,可以看到只初始化了debounceTimeoutIds属性:
debounceTimeout存储
When the call is
@debounce
during the method of labeling:
.. 1 the setTimeout delays execution of the function, the object returns a timeout;
2 timeout object returned will be stored in.debounceTimeoutIds[key]
(InupdateContent
the method as an example,debounceTimeoutIds[key]
Key value is hereupdateContent
);
3 repeat the call. this method will be re-created timeout object before and coverdebounceTimeoutIds[key]
(where each callupdateContent
will be to re-debounceTimeoutIds['updateContent']
assignment);
1 |
setTimeout(() => { |
Implementation of the results is this: every time again wait wait, go perform the callback method, callback method that is key corresponding to the value of .
Original: Large column core-decorators Source Analysis of --debounce