From 23b22d97db0a29c33e9f509c4eb8dce63ae9211e Mon Sep 17 00:00:00 2001 From: saqqdy Date: Mon, 18 Nov 2024 08:47:44 +0800 Subject: [PATCH] got returns of PunctualTimerReturns --- CHANGELOG.md | 4 ++++ README.md | 19 +++++++++++++++---- package.json | 2 +- src/index.ts | 2 +- src/punctualTimer.ts | 42 +++++++++++++++++++++++++++++++++--------- src/randomString.ts | 2 +- src/spliceUrlParam.ts | 2 +- 7 files changed, 56 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd89c349..2eb72f40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change logs +## 2024.11.18 v5.23.0 + +1. `punctualTimer` got returns of PunctualTimerReturns + ## 2024.11.15 v5.22.1 1. fix bugs of `nextVersion` diff --git a/README.md b/README.md index 64c5a0ca..4b31cc39 100644 --- a/README.md +++ b/README.md @@ -2557,6 +2557,8 @@ declare function nextVersion( punctual setInterval +> v5.23.0 got returns of PunctualTimerReturns + - Since: `5.18.0` - Arguments: @@ -2567,13 +2569,15 @@ punctual setInterval | delay | The time, in milliseconds that the timer should wait before the specified function or code is executed. If this parameter is omitted, a value of 0 is used, meaning execute "immediately", or more accurately, the next event cycle. | `number` | - | `true` | - | | ...args | Additional arguments which are passed through to the function specified by handler. | `any[]` | - | `false` | - | -- Returns: `void` +- Returns: `PunctualTimerReturns` - Example: ```ts const printDate = () => console.log(new Date()) -punctualTimer(printDate, 1000) +const timer = punctualTimer(printDate, 1000) +console.log(timer.count) // 10 +timer.clear() // clear punctualTimer or use clearTimeout(timer.timer) ``` - Types: @@ -2583,12 +2587,19 @@ declare function punctualTimer( handler: (args: void) => void, delay: number, [...args]?: TArgs -): void +): PunctualTimerReturns + declare function punctualTimer( handler: (...args: TArgs) => void, delay: number, [...args]?: TArgs -): void +): PunctualTimerReturns + +declare interface PunctualTimerReturns { + count: number + timer: number | null + clear: () => void +} ``` #### promiseFactory diff --git a/package.json b/package.json index fd82cf48..d1aeb43a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "js-cool", "description": "Collection of common JavaScript / TypeScript utilities", - "version": "5.22.1", + "version": "5.23.0", "packageManager": "pnpm@9.1.3", "main": "dist/index.cjs.js", "module": "dist/index.esm-bundler.js", diff --git a/src/index.ts b/src/index.ts index de7f2136..2b64d13b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -112,7 +112,7 @@ export { default as getScrollPosition } from './getScrollPosition' // tools export { default as nextIndex } from './nextIndex' export { default as nextVersion, type Version } from './nextVersion' -export { default as punctualTimer } from './punctualTimer' +export { default as punctualTimer, type PunctualTimerReturns } from './punctualTimer' export { default as promiseFactory } from './promiseFactory' export { default as fixNumber } from './fixNumber' export { default as mapTemplate } from './mapTemplate' diff --git a/src/punctualTimer.ts b/src/punctualTimer.ts index 73c684c6..d9766dee 100644 --- a/src/punctualTimer.ts +++ b/src/punctualTimer.ts @@ -1,10 +1,18 @@ +export interface PunctualTimerReturns { + count: number + timer: number | null + clear: () => void +} + /** * punctual setInterval * * @example * ```js * const printDate = () => console.log(new Date()) - * punctualTimer(printDate, 1000) + * const timer = punctualTimer(printDate, 1000) + * console.log(timer.count) // 10 + * timer.clear() // clear punctualTimer or use clearTimeout(timer.timer) * ``` * @since 5.18.0 * @param handler - A function to be executed after the timer expires. @@ -15,25 +23,41 @@ function punctualTimer( handler: (args: void) => void, delay: number, [...args]?: TArgs -): void +): PunctualTimerReturns function punctualTimer( handler: (...args: TArgs) => void, delay: number, [...args]?: TArgs -): void -function punctualTimer(handler: any, delay: number, ...args: TArgs) { +): PunctualTimerReturns +function punctualTimer( + handler: any, + delay: number, + ...args: TArgs +): PunctualTimerReturns { handler() - let counter = 1 + let _this: PunctualTimerReturns = { + count: 1, + timer: null, + clear() { + if (this.timer) { + clearTimeout(this.timer) + this.timer = null + } + _this = null as any + return _this + } + } const start = new Date().getTime() const instance = () => { handler() - const ideal = counter * delay + const ideal = _this.count * delay const real = new Date().getTime() - start - counter++ + _this.count++ const diff = real - ideal - setTimeout(instance, delay - diff, ...args) // Repair by system time + _this.timer = setTimeout(instance, delay - diff, ...args) // Repair by system time } - setTimeout(instance, delay, ...args) + _this.timer = setTimeout(instance, delay, ...args) + return _this } export default punctualTimer diff --git a/src/randomString.ts b/src/randomString.ts index 587d80e2..c9ab715d 100644 --- a/src/randomString.ts +++ b/src/randomString.ts @@ -90,7 +90,7 @@ function randomString( result = '' if (typeof len !== 'number') { options = len - len = typeof options === 'object' ? options.length ?? 32 : 32 // default + len = typeof options === 'object' ? (options.length ?? 32) : 32 // default } if (typeof options === 'boolean') { if (options) charTypes.push('special') diff --git a/src/spliceUrlParam.ts b/src/spliceUrlParam.ts index 430336ea..14475a4d 100644 --- a/src/spliceUrlParam.ts +++ b/src/spliceUrlParam.ts @@ -44,7 +44,7 @@ function spliceUrlParam>( const result: string[] = [] for (key in params) { if (typeof key === 'string') { - const val = '' + (covert ? params[key] ?? '' : params[key]) + const val = '' + (covert ? (params[key] ?? '') : params[key]) result.push(`${key}=${encode ? encodeURIComponent(val) : val}`) } }