Skip to content

Commit

Permalink
fix(pref-setinterval): clearPrefSetInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
fupengl committed Jun 15, 2021
1 parent 2445cb1 commit 10df8ca
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/function/pref-setInterval.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { prefSetTimeout, clearPrefTimeout } from './pref-setTimeout';
import incrementId from './increment-id';

const id = incrementId();

const timerIdMap: Record<number, number> = {};

/**
* 优先使用 requestAnimationFrame 实现 setInterval
Expand All @@ -10,11 +15,13 @@ import { prefSetTimeout, clearPrefTimeout } from './pref-setTimeout';
*/
export function prefSetInterval(handler: Function, ms?: number, ...args: any[]): number {
const interval = ms || 0;
const _id = id();
function loop() {
return prefSetTimeout(() => {
timerIdMap[_id] = prefSetTimeout(() => {
handler(...args);
loop();
}, interval);
return _id;
}
return loop();
}
Expand All @@ -24,5 +31,8 @@ export function prefSetInterval(handler: Function, ms?: number, ...args: any[]):
* @param handle
*/
export function clearPrefSetInterval(handle: number) {
clearPrefTimeout(handle);
if (timerIdMap[handle]) {
clearPrefTimeout(handle);
delete timerIdMap[handle];
}
}

0 comments on commit 10df8ca

Please sign in to comment.