小程序防抖功能
util.js
// 防抖
export const debounce = (fn, interval)=>{var timer;var gapTime = interval || 1000;//間隔時間,如果interval不傳,則默認(rèn)1000msreturn function() {clearTimeout(timer);var context = this;var args = arguments;//保存此處的arguments,因為setTimeout是全局的,arguments不是防抖函數(shù)需要的。timer = setTimeout(function() {fn.call(context,args);}, gapTime);};
}
在需要防抖的頁面引入
import {debounce } from "../../utils/util" Page({// 刪除審核未通過的del:debounce(function () {wx.showLoading({title: '刪除中',})fetch.post({url:api.flatAvatarDelete,data:{audit_id:this.data.mateInfo.audit_id},success:(res)=> {const lastPage = getCurrentPages()[0]lastPage.needRefresh = truewx.showToast({title: '刪除成功',icon: 'success',duration: 2000})wx.navigateBack()},fail: (err)=> {wx.showToast({title: err.data.message,icon: 'none',duration: 2000})},})}), })總結(jié)
- 上一篇: 针对不同场景的Python合并多个Exc
- 下一篇: 电脑上如何进行MP4格式转换成其它格式?