防抖节流装饰器
This commit is contained in:
		| @@ -45,15 +45,14 @@ export function loading() { | ||||
|  * @returns {(function(*, *, *): void)|*} | ||||
|  */ | ||||
| export function throttle(wait) { | ||||
|   return function (target, name, descriptor) { | ||||
|   let timer; | ||||
|   return function (t, n, descriptor) { | ||||
|     const origin = descriptor.value | ||||
|     let lock = false | ||||
|     descriptor.value = function () { | ||||
|       if (!lock) { | ||||
|         lock = true | ||||
|         origin.apply(this, arguments) | ||||
|       if (!timer) { | ||||
|         setTimeout(() => { | ||||
|           lock = false | ||||
|           origin.apply(this, arguments) | ||||
|           timer = null | ||||
|         }, wait) | ||||
|       } | ||||
|     } | ||||
| @@ -83,3 +82,21 @@ export function load(sdk, interval = 200, name = "") { | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * 防抖装饰器 | ||||
|  * @param delay 防抖时间 | ||||
|  * @returns {(function(*, *, *): void)|*} | ||||
|  */ | ||||
| export function debounce(delay) { | ||||
|   let timer = null; | ||||
|   return function (t, n, descriptor) { | ||||
|     const origin = descriptor.value | ||||
|     descriptor.value = function () { | ||||
|       clearTimeout(timer); | ||||
|       timer = setTimeout(() => { | ||||
|         origin.apply(this, arguments) | ||||
|       }, delay) | ||||
|     } | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user