Skip to content

Commit d00337a

Browse files
committed
Clear timers and event listeners before component unmounts
1 parent b62aa87 commit d00337a

File tree

6 files changed

+51
-9
lines changed

6 files changed

+51
-9
lines changed

docs/src/components/CodeExample.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import IconJavaScript from "./IconJavaScript.vue"
1212
import IconSvelte from "./IconSvelte.vue"
1313
import IconAngular from "./IconAngular.vue"
1414
import IconNuxt from "./IconNuxt.vue"
15-
import { computed, ref } from "vue"
15+
import { computed, ref, onBeforeUnmount } from "vue";
1616
import { vAutoAnimate } from "../../../src"
1717
import "../../assets/prism.css"
1818
19+
const timeOutID = ref();
20+
1921
type LanguageOption =
2022
| "react"
2123
| "preact"
@@ -77,10 +79,14 @@ const copyStatus = ref(false)
7779
function copyCode(value: string) {
7880
window.navigator.clipboard.writeText(value)
7981
copyStatus.value = true
80-
setTimeout(() => {
82+
timeOutID.value = setTimeout(() => {
8183
copyStatus.value = false
8284
}, 2000)
8385
}
86+
87+
onBeforeUnmount(() => {
88+
clearTimeout(timeOutID.value);
89+
});
8490
</script>
8591

8692
<template>

docs/src/components/Navigation.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<script setup lang="ts">
2-
import { ref } from "vue"
2+
import { ref, onBeforeUnmount } from "vue";
33
import IconDown from "./IconDown.vue"
44
import PoliteAd from "./PoliteAd.vue"
55
import { vAutoAnimate } from "../../../src/index"
66
7+
const controller = new window.AbortController();
8+
79
const show = ref(false)
810
const activeTitle = ref("Docs Navigation")
911
@@ -25,8 +27,18 @@ if (typeof window !== "undefined") {
2527
clearTimeout(resizeTimer)
2628
window.addEventListener("resize", () => {
2729
resizeTimer = setTimeout(applySizing, 200)
30+
31+
this.$once('hook:beforeUnmount', () => {
32+
clearTimeout(resizeTimer);
33+
});
34+
}, {
35+
signal: controller?.signal
2836
})
2937
}
38+
39+
onBeforeUnmount(() => {
40+
controller.abort();
41+
});
3042
</script>
3143

3244
<template>

docs/src/components/TheLogo.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ onMounted(() => {
1313
clearInterval(interval)
1414
}
1515
}, duration)
16+
17+
this.$once('hook:beforeUnmount', () => {
18+
clearInterval(interval);
19+
});
1620
})
1721
</script>
1822

docs/src/examples/cards/ActualCards.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<script setup>
2-
import { ref } from "vue"
2+
import { ref, onBeforeUnmount } from "vue";
33
import { vAutoAnimate } from "../../../../src/index"
44
5+
const timeOutID = ref();
6+
57
const showForm = ref(false)
68
const cards = ref([
79
{
@@ -20,10 +22,14 @@ const cards = ref([
2022
2123
function createCard(card) {
2224
cards.value.unshift(card)
23-
setTimeout(() => {
25+
timeOutID.value = setTimeout(() => {
2426
showForm.value = false
2527
}, 300)
2628
}
29+
30+
onBeforeUnmount(() => {
31+
clearTimeout(timeOutID.value);
32+
});
2733
</script>
2834

2935
<template>

docs/src/examples/disable/ActualDisable.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<script setup lang="ts">
2-
import { ref } from "vue"
2+
import { ref, onBeforeUnmount } from "vue";
33
import { useAutoAnimate } from "../../../../src/vue"
44
5+
const intervalID = ref();
6+
57
const balls = ref(["red", "green", "blue"])
68
const [parent, enable] = useAutoAnimate({ duration: 500 })
79
@@ -10,9 +12,14 @@ function toggle() {
1012
isEnabled.value ? enable(false) : enable(true)
1113
isEnabled.value = !isEnabled.value
1214
}
13-
setInterval(() => {
15+
16+
intervalID.value = setInterval(() => {
1417
balls.value.push(balls.value.shift()!)
1518
}, 600)
19+
20+
onBeforeUnmount(() => {
21+
clearInterval(intervalID.value);
22+
});
1623
</script>
1724

1825
<template>

docs/src/examples/disable/disable.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<script setup>
2-
import { ref } from "vue"
2+
import { ref, onBeforeUnmount } from "vue";
33
import { useAutoAnimate } from "../../../../src/vue"
44
5+
const intervalID = ref();
6+
57
const balls = ref(["red", "green", "blue"])
68
const [parent, enable] = useAutoAnimate({ duration: 500 })
79
@@ -10,9 +12,14 @@ function toggle() {
1012
isEnabled.value ? enable(false) : enable(true)
1113
isEnabled.value = !isEnabled.value
1214
}
13-
setInterval(() => {
15+
16+
intervalID.value = setInterval(() => {
1417
balls.value.push(balls.value.shift()!)
1518
}, 600)
19+
20+
onBeforeUnmount(() => {
21+
clearInterval(intervalID.value);
22+
});
1623
</script>
1724

1825
<template>

0 commit comments

Comments
 (0)