ph-utils 0.2.11 → 0.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -58,4 +58,4 @@ export declare class BaseError extends Error {
58
58
  * @param wait 需要节流的毫秒
59
59
  * @returns
60
60
  */
61
- export declare function throttle<T extends (...args: any) => any>(func: T, wait: number): (...args: any[]) => void;
61
+ export declare function throttle<T extends (...args: any) => any>(func: T, wait?: number): (...args: any[]) => void;
package/lib/index.js CHANGED
@@ -83,14 +83,13 @@ exports.BaseError = BaseError;
83
83
  * @param wait 需要节流的毫秒
84
84
  * @returns
85
85
  */
86
- function throttle(func, wait) {
87
- let lastTime = 0;
86
+ function throttle(func, wait = 300) {
87
+ let t = -1;
88
88
  return function (...args) {
89
- let now = Date.now();
90
- if (now - lastTime >= wait) {
89
+ clearTimeout(t);
90
+ t = setTimeout(() => {
91
91
  func(...args);
92
- lastTime = now;
93
- }
92
+ }, wait);
94
93
  };
95
94
  }
96
95
  exports.throttle = throttle;
package/lib/index_m.d.ts CHANGED
@@ -58,4 +58,4 @@ export declare class BaseError extends Error {
58
58
  * @param wait 需要节流的毫秒
59
59
  * @returns
60
60
  */
61
- export declare function throttle<T extends (...args: any) => any>(func: T, wait: number): (...args: any[]) => void;
61
+ export declare function throttle<T extends (...args: any) => any>(func: T, wait?: number): (...args: any[]) => void;
package/lib/index_m.js CHANGED
@@ -70,13 +70,12 @@ export class BaseError extends Error {
70
70
  * @param wait 需要节流的毫秒
71
71
  * @returns
72
72
  */
73
- export function throttle(func, wait) {
74
- let lastTime = 0;
73
+ export function throttle(func, wait = 300) {
74
+ let t = -1;
75
75
  return function (...args) {
76
- let now = Date.now();
77
- if (now - lastTime >= wait) {
76
+ clearTimeout(t);
77
+ t = setTimeout(() => {
78
78
  func(...args);
79
- lastTime = now;
80
- }
79
+ }, wait);
81
80
  };
82
81
  }
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "module": "lib/index_m.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "browser": "lib/index_m.js",
8
- "version": "0.2.11",
8
+ "version": "0.2.12",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https//gitee.com/towardly/ph.git",