hookehuyr

✨ feat: 简单封装debounce防抖函数

...@@ -5,9 +5,8 @@ ...@@ -5,9 +5,8 @@
5 <div class="video-bar"> 5 <div class="video-bar">
6 <van-row> 6 <van-row>
7 <van-col span="12" @click="goTo"> 7 <van-col span="12" @click="goTo">
8 - <van-image v-if="item.avatar" round width="2rem" height="2rem" style="vertical-align: middle;" :src="item.avatar" />&nbsp; 8 + <van-image round width="2rem" height="2rem" style="vertical-align: middle;" :src="item.avatar ? item.avatar : icon_avatar" />
9 - <van-image v-else round width="2rem" height="2rem" style="vertical-align: middle;" :src="icon_avatar" />&nbsp; 9 + <span style="font-size: 1.05rem;vertical-align: middle; display: inline-block; padding-left: 1rem;">{{ item.name }}</span>
10 - <span style="font-size: 1.05rem;vertical-align: middle;">{{ item.name }}</span>
11 </van-col> 10 </van-col>
12 <van-col span="12"> 11 <van-col span="12">
13 <div style="padding: 0.25rem; padding-top: 0.75rem; text-align: right;"> 12 <div style="padding: 0.25rem; padding-top: 0.75rem; text-align: right;">
...@@ -42,11 +41,15 @@ import { useRouter } from 'vue-router' ...@@ -42,11 +41,15 @@ import { useRouter } from 'vue-router'
42 import 'mui-player/dist/mui-player.min.css' 41 import 'mui-player/dist/mui-player.min.css'
43 import MuiPlayer from 'mui-player' 42 import MuiPlayer from 'mui-player'
44 import { prodActionAPI, prodInfoAPI } from '@/api/C/prod.js' 43 import { prodActionAPI, prodInfoAPI } from '@/api/C/prod.js'
44 +import { useDebounce } from '@/hooks/useDebounce.js'
45 45
46 const $router = useRouter(); 46 const $router = useRouter();
47 const props = defineProps({ 47 const props = defineProps({
48 item: { 48 item: {
49 type: Object, 49 type: Object,
50 + default(rawProps) {
51 + return rawProps
52 + }
50 } 53 }
51 }) 54 })
52 55
...@@ -55,7 +58,7 @@ const props = defineProps({ ...@@ -55,7 +58,7 @@ const props = defineProps({
55 * 两个请求顺序执行,处理直接写在请求函数里面似乎有点问题,get请求数据似乎无法顺利渲染显示 58 * 两个请求顺序执行,处理直接写在请求函数里面似乎有点问题,get请求数据似乎无法顺利渲染显示
56 */ 59 */
57 // FIXME:防抖处理 60 // FIXME:防抖处理
58 -const handleAction = _.debounce(async (action_type, prod_id) => { 61 +const handleAction = useDebounce(async (action_type, prod_id) => {
59 const { msg } = await prodActionAPI({ action_type, prod_id }); 62 const { msg } = await prodActionAPI({ action_type, prod_id });
60 if (msg === `${action_type}-add-OK`) { // 动作操作成功 63 if (msg === `${action_type}-add-OK`) { // 动作操作成功
61 if (action_type === 'favor') { 64 if (action_type === 'favor') {
...@@ -70,7 +73,7 @@ const handleAction = _.debounce(async (action_type, prod_id) => { ...@@ -70,7 +73,7 @@ const handleAction = _.debounce(async (action_type, prod_id) => {
70 } 73 }
71 } 74 }
72 getProductDetail(action_type, prod_id); // 更新信息 75 getProductDetail(action_type, prod_id); // 更新信息
73 -}, 500, { leading: true, trailing: false }); 76 +});
74 77
75 // 查询作品详情 78 // 查询作品详情
76 const getProductDetail = async (action_type, prod_id) => { 79 const getProductDetail = async (action_type, prod_id) => {
...@@ -142,7 +145,6 @@ const setComment = () => { ...@@ -142,7 +145,6 @@ const setComment = () => {
142 color: #713610; 145 color: #713610;
143 padding: 1rem; 146 padding: 1rem;
144 padding-bottom: 0.5rem; 147 padding-bottom: 0.5rem;
145 -
146 } 148 }
147 } 149 }
148 </style> 150 </style>
......
1 +import _ from 'lodash';
2 +/**
3 + * 封装lodash防抖
4 + * @param {*} fn 执行函数
5 + * @param {*} timestamp 执行间隔
6 + * @param {*} options 函数配置 - 在延迟开始前调用,在延迟结束后不调用
7 + * @returns 返回新的 debounced(防抖动)函数。
8 + */
9 +export const useDebounce = (fn, timestamp = 500, options = { leading: true, trailing: false }) => {
10 + return _.debounce(fn, timestamp, options);
11 +}