当前位置:网站首页>Elegant throttling / de buffeting decorator under LAYA
Elegant throttling / de buffeting decorator under LAYA
2022-06-12 00:59:00 【King Lear 】
There are a lot of articles about decorators , I won't describe it in detail here . Decorators can only decorate classes and class attributes .
1. install tslib
tslib Is a runtime library , It includes all TypeScript Auxiliary function .
The library needs to be tsconfig Open in –importHelpers Logo use .
Under project directory : npm i tslib
2. To configure tsconfig
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noEmitHelpers": true,
"sourceMap": true,
"importHelpers": true,
"experimentalDecorators": true,
"baseUrl": ".", // It's necessary , otherwise paths Configuration will not take effect
"paths": {
"tslib": [
"tslib/tslib.d.ts"
]
}
}
3. Examples of decorators
When decorating class attributes , You need to use three parameters in the decorator function :target、name、descriptor, They represent the target classes that need to be modified 、 Target properties 、 Descriptor of the target attribute .
When writing decorators, be sure to make it clear this Several rules pointing to :
- When a function uses
newOperator , Inside the functionthisPoints to the newly created object . - When using
callorapplyWhen you call a function ,thisYescallorapplyThe first parameter specified . If the first parameter isnullOr when it is not an object ,thisPoint to global object . - When a function is called as an attribute in an object , Inside a function
thisPoints to the corresponding object . - Finally, the default rule , Default
thisPoint to global object , In the browser Window. But in strict mode it is forundefinedOf .
/** * Function throttling decorator * @param wait: Save time */
export function throttle(wait: number) {
let previous = 0;
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
const oldValue = descriptor.value
descriptor.value = function (...args) {
const now = Date.now();
if (now - previous > wait) {
oldValue.apply(this, args)
previous = now;
}
};
return descriptor
}
}
/** * Function to dither decorator ( Executive now ) * @param wait: Shake the time */
export function debounce(wait: number) {
let timeout;
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
const oldValue = descriptor.value
descriptor.value = function (...args) {
if (timeout) clearTimeout(timeout);
let callNow = !timeout;
timeout = setTimeout(() => {
timeout = null;
}, wait)
if (callNow) oldValue.apply(this, args)
};
return descriptor
}
}
4. Usage method
Elegant use of restrictors
@throttle(500) function : After the function is triggered, the limit 500ms Can be triggered again . Application scenarios : Callbacks that can usually be used to limit too fast clicks .
@debounce(500) function : After the function is triggered, the limit 500ms It can only be triggered again if it has not been called in ( Executive now ). Application scenarios : It can usually be used to limit mousemove event , Input events, etc. in the input box . They trigger very frequently , At this time, the function does not need to be executed all the time . Function anti shake is to let events trigger n Only once per second , If it is triggered continuously, the time will be recalculated .
@throttle(500)
private onTimeBtn() {
DialogView.show({
content: i18n(" Are you sure to refresh the current store ?"),
okFunc: Laya.Handler.create(this, () => {
this.refreshShop();
})
})
}
边栏推荐
- DevOps落地实践点滴和踩坑记录-(1)
- [answer] is ubiquitous language a pseudo innovation?
- 一、Flutter 入门学习写一简单客户端
- Jeecgboot 3.1.0 release, enterprise low code platform based on code generator
- Water for a while
- Recurrent+Transformer 视频恢复领域的‘德艺双馨’
- 2022 edition of global and Chinese hexamethylene chloride industry dynamic research and investment prospect forecast report
- [case] building a universal data lake for Fuguo fund based on star ring technology data cloud platform TDC
- Lambda中间操作filter
- Chapter V - Fund professional ethics
猜你喜欢

Nat. Comm. | supercomputing +ai: providing navigation for natural product biosynthesis route planning

Explore table component virtualization

Building circuits on glass

Ms-hgat: information diffusion prediction based on memory enhanced sequence hypergraph attention network

KV storage separation principle and performance evaluation of nebula graph

Streaming Data Warehouse 存储:需求与架构

Devops landing practice drip and pit stepping records - (1)

Codemirror 2 - highlight only (no editor) - codemirror 2 - highlight only (no editor)

Enterprise wechat H5_ Integrated message decryption class, message push get and post callback processing

详解异步任务:函数计算的任务触发去重
随机推荐
中创专利|中国5G标准必要专利达1.8万项,尊重知识产权,共建知识产权强国
[answer] should the role with one end of the reflexive association be called "current version"
[case] building a universal data lake for Fuguo fund based on star ring technology data cloud platform TDC
Investment analysis and demand forecast report of global and Chinese fluorosilicone industry in 2022
LabVIEW Arduino electronic weighing system (project Part-1)
[工具] spacing.js 测间距
河南中创|从云到边,边缘计算如何赋能数据中心
JDBC exercises
Intel trimbert: tailor Bert for trade-offs
Equipment encryption of industrial control security
C language bit operation - learning 26
What is bonded warehouse and what is the difference between them
ARP instruction
Big sword
be based on. NETCORE development blog project starblog - (11) access statistics
Global and Chinese chromatographic silica gel resin industry research and investment direction forecast report 2022 Edition
深度学习100例 | 第41天:语音识别 - PyTorch实现
Breadth first search depth first search dynamic programming leetcode topic: delivering information
[answer] in the business sequence diagram of XX shopping mall, is it drawn as a business executor (bank) or a business entity (banking system)
Low code platform design exploration, how to better empower developers