一、获取防抖函数的返回值
js
/**
* 创建一个防抖函数,该函数返回一个新的防抖版本的函数。
* 为了获取函数的返回值
* @returns {Function} 返回一个新的防抖函数。
*/
export function debounceReturnFn() {
let timer: any = null
/**
* 新的防抖函数,接收一个函数、等待时间和任意参数,返回一个Promise。
* @param {Function} fn 需要防抖的函数
* @param {number} wait 防抖的等待时间
* @param {...any} args 传递给函数的参数
* @returns {Promise} 返回一个Promise,该Promise在指定时间后执行或者在出错时拒绝。
*/
const newDebounce = function(fn: any, wait: number, ...args: any) {
return new Promise((resolve, reject) => {
if (timer !== null) {
clearTimeout(timer)
}
timer = setTimeout((_: any) => {
try {
resolve(fn(...args))
} catch (e) {
reject(e)
}
}, wait)
})
}
return newDebounce
}
html
import { debounceReturnFn } from './common'
private newDebounce: any = debounceReturnFn()
private async searchActCharge(query: string) {
this.activityInChargeList = await this.newDebounce(switchChargeList, 500, {
type: 'activityInCharge',
query,
})
}
/**
* 返回 下拉列表信息
* @param type
* @param query
* @returns
*/
export const switchChargeList = ({ type, query }: MisChargeType) => {
switch (type) {
case 'activityInCharge':
if (query) {
return searchMisHandle({ type, query })
}
return []
}
}
/**
* 调用 接口信息
* @param type
* @param query
* @returns
*/
export const searchMisHandle = async ({ type, query }: { type: string; query: string }) => {
switch (type) {
case 'activityInCharge':
var activityInChargeList: SearchMisType[] = []
activityInChargeList = await apiGetByMisHandle(query)
return activityInChargeList
}
}
/**
* 接口请求返回
* @param query
* @returns
*/
export async function apiGetByMisHandle(query: string) {
const params = {
mis: query,
}
const res = await net.activityRisk.apiGetByMis(params)
if (res.data?.data) {
const arr = []
arr.push(res.data?.data)
return arr
}
return []
}