Skip to content

Commit

Permalink
[fix] Fix program lag issue
Browse files Browse the repository at this point in the history
  • Loading branch information
hbl917070 committed Nov 21, 2023
1 parent b242358 commit d19562f
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions Www/ts/Lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,25 +720,28 @@ interface Date {
* 節流 (定時執行,時間內重複執行,則只會執行最後一個指令)
*/
class Throttle {
public run: (() => void) | undefined = undefined;
public timeout = 50;
public run: (() => Promise<void>) | 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);
}
}

0 comments on commit d19562f

Please sign in to comment.