-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: can not stop calling the 'update' method when elapsed > duration…
- Loading branch information
1 parent
f618a3a
commit 1a50977
Showing
3 changed files
with
47 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,41 @@ | ||
import { ref, onUnmounted } from 'vue' | ||
|
||
import { ref, onUnmounted, computed } from 'vue' | ||
export default { | ||
setup() { | ||
const duration = ref(15 * 1000) | ||
const elapsed = ref(0) | ||
|
||
let lastTime = performance.now() | ||
let lastTime | ||
let handle | ||
|
||
const update = () => { | ||
const time = performance.now() | ||
elapsed.value += Math.min(time - lastTime, duration.value - elapsed.value) | ||
lastTime = time | ||
handle = requestAnimationFrame(update) | ||
elapsed.value = performance.now() - lastTime | ||
if (elapsed.value >= duration.value) { | ||
cancelAnimationFrame(handle) | ||
} else { | ||
handle = requestAnimationFrame(update) | ||
} | ||
} | ||
|
||
const reset = () => { | ||
elapsed.value = 0 | ||
lastTime = performance.now() | ||
update() | ||
} | ||
|
||
update() | ||
const progressRate = computed(() => | ||
Math.min(elapsed.value / duration.value, 1) | ||
) | ||
|
||
reset() | ||
|
||
onUnmounted(() => { | ||
cancelAnimationFrame(handle) | ||
}) | ||
|
||
return { | ||
duration, | ||
elapsed | ||
elapsed, | ||
progressRate, | ||
reset | ||
} | ||
} | ||
} |
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