diff --git a/Www/ts/Lib.ts b/Www/ts/Lib.ts index bebeed7..b3f526c 100644 --- a/Www/ts/Lib.ts +++ b/Www/ts/Lib.ts @@ -720,25 +720,28 @@ interface Date { * 節流 (定時執行,時間內重複執行,則只會執行最後一個指令) */ class Throttle { - public run: (() => void) | undefined = undefined; - public timeout = 50; + public run: (() => Promise) | undefined = undefined; - constructor(_timeout = 50) { - this.timeout = _timeout; - this.timer(); - } + constructor(timeout = 50) { + + let isAsyncTaskRunning = false; + + setInterval(() => { - private async timer() { + if (this.run === undefined) { return; } + if (isAsyncTaskRunning) { return; } - if (this.run !== undefined) { - let _func: (() => void) | undefined = this.run; + let func = this.run; this.run = undefined; - await _func(); - _func = undefined; - } + isAsyncTaskRunning = true; + + func().then(() => { + isAsyncTaskRunning = false; + }).catch(() => { + console.error(); + isAsyncTaskRunning = false; + }); - // 遞迴 - await Lib.sleep(this.timeout); - this.timer(); + }, timeout); } }