feat: debounce & thtottle (#244)

This commit is contained in:
一万
2022-04-19 11:17:23 +08:00
committed by GitHub
parent a80c6f034a
commit db09d1c442
7 changed files with 101 additions and 15 deletions

View File

@@ -0,0 +1,52 @@
<script setup lang="ts">
import { ElMessage } from "element-plus";
import { debounce } from "/@/utils/debounce";
import { useDebounceFn, useThrottleFn } from "@vueuse/core";
const handle = () => {
ElMessage({
message: "恭喜你,这是一条成功消息",
type: "success"
});
};
const immediateDebounce = debounce(handle, 1000, true);
const debounceClick = useDebounceFn(handle, 1000);
const throttleClick = useThrottleFn(handle, 1000, false);
</script>
<template>
<div>
<el-card class="mb-5">
<template #header>
<div>防抖debounce</div>
</template>
<div class="mb-5">
所谓防抖就是指触发事件后在 n 秒内函数只能执行一次如果在 n
秒内又触发了事件则会重新计算函数执行时间
</div>
<el-button @click="immediateDebounce"
>连续点击我只会执行最后一次点击事件立即执行</el-button
>
<el-button @click="debounceClick"
>连续点击我只会执行最后一次点击事件延后执行</el-button
>
</el-card>
<el-card>
<template #header>
<div>节流throttle</div>
</template>
<div class="mb-5">
所谓节流就是指连续触发事件但是在 n
秒中只执行一次函数节流会稀释函数的执行频率
</div>
<el-button @click="throttleClick"
>连续点击我每一秒只会执行一次点击事件</el-button
>
</el-card>
</div>
</template>
<style scoped></style>

View File

@@ -55,11 +55,7 @@ function onCloseTags() {
<template>
<el-card>
<template #header>
<div class="card-header">
<span class="font-medium"
>标签页复用超出限制自动关闭使用场景: 动态路由</span
>
</div>
<div>标签页复用超出限制自动关闭使用场景: 动态路由</div>
</template>
<el-button v-for="index in 6" :key="index" @click="toDetail(index)">
打开{{ index }}详情页