表单内容改变 节流操作

一般情况下输入内容后,筛选对应的数据内容,如果每次输入每次请求过于频繁,使用节流的方式来减少请求的情况;

watch: {
	  'params.search'(newVal,oldVal){
		  //添加空格不做处理
		  if(newVal.trim() == oldVal.trim()) return;
		  // 输入框防抖 
		  clearTimeout(this.debounceTimer);
		  // 每次进来的时候都将之前的清除掉,如果还没到毫秒的时候就将之前的清除掉,
		  // 这样就不会触发之前setTimeout绑定的事件, 
		  // 如果超过毫秒,之前的事件就会被触发下次进来的时候同样清除之前的timer
		  this.debounceTimer = setTimeout(()=>{
			  //输入框没值了,清空搜索框条件数据
			  this.getStorkList(this.params);
		  },500) //几毫秒进行
	  }

	},

方法二:


let _throttleRunning = null, _debounceTimeout = null
/**
 * 防抖
 * @param {Function} 执行函数
 * @param {Number} delay 延时ms
 */
export const debounce = (fn, delay= 500) => {
  clearTimeout(_debounceTimeout)
  _debounceTimeout = setTimeout(() => {
    fn()
  }, delay)
}
/**
 * 节流
 * @param {Function} 执行函数
 * @param {Number} delay 延时ms
 */
export const throttle = (fn, delay= 500) => {
  if (_throttleRunning) {
    return
  }
  _throttleRunning = true
  fn()
  setTimeout(() => {
    _throttleRunning = false
  }, delay)
}

方法三


let timerNull = null
export function debounceAnd(func, wait, immediate = false) {
  if(timerNull) clearTimeout(timerNull)
  if (immediate) {
    // 节流
    let callNow = !timerNull
    timerNull = setTimeout(() => {
      timerNull = null
    }, wait)
    if (callNow) typeof func === 'function' && func();
  } else {
    // 防抖
    timerNull = setTimeout(() => {
      typeof func === 'function' && func()
    }, wait)
  }
}

仅供参考,内容待完善!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值