-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ♻️ 提供useRaf并重构useCountDown计时逻辑
- Loading branch information
1 parent
0e6ea3e
commit 661603a
Showing
5 changed files
with
77 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/uni_modules/wot-design-uni/components/composables/useRaf.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ref, onUnmounted } from 'vue' | ||
import { isDef, isH5, isNumber } from '../common/util' | ||
|
||
// 定义回调函数类型 | ||
type RafCallback = (time: number) => void | ||
|
||
export function useRaf(callback: RafCallback) { | ||
const requestRef = ref<number | null | ReturnType<typeof setTimeout>>(null) | ||
|
||
// 启动动画帧 | ||
const start = () => { | ||
const handle = (time: number) => { | ||
callback(time) | ||
} | ||
|
||
if (isH5) { | ||
requestRef.value = requestAnimationFrame(handle) | ||
} else { | ||
requestRef.value = setTimeout(() => handle(Date.now()), 1000 / 30) | ||
} | ||
} | ||
|
||
// 取消动画帧 | ||
const cancel = () => { | ||
if (isH5 && isNumber(requestRef.value)) { | ||
cancelAnimationFrame(requestRef.value!) | ||
} else if (isDef(requestRef.value)) { | ||
clearTimeout(requestRef.value) | ||
} | ||
} | ||
|
||
onUnmounted(() => { | ||
cancel() | ||
}) | ||
|
||
return { start, cancel } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters