1. 防暴点(preventReClick)

快速点击按钮时会频繁重复调用接口,为了防止这种情况,要对相应的按钮进行防暴力重复点击处理,最好是使用函数防抖或者函数截流来处理,但是现在已经要修改的按钮太多了,于是换一种方法,用指令的方式来达到相识的效果。
1.创建utils文件夹,在该文件夹下创建preventReClick.js文件
JavaScript代码
  1. export default {  
  2.     install(Vue) {  
  3.         Vue.directive('preventReClick', {  
  4.             inserted: function (el, binding) {  
  5.                 console.log(el.disabled)  
  6.                 el.addEventListener('click', (e) => {  
  7.                     if (!el.disabled) {  
  8.                         el.disabled = true  
  9.                         setTimeout(() => {  
  10.                             el.disabled = false  
  11.                         }, binding.value || 3000)  
  12.                         //binding.value可以自行设置。如果设置了则跟着设置的时间走  
  13.                         //例如:v-preventReClick='500'  
  14.                     } else { // disabled为true时,阻止默认的@click事件  
  15.                         e.preventDefault()  
  16.                         e.stopPropagation()  
  17.                     }  
  18.                 })  
  19.             }  
  20.         }),  
  21.     }  
2.在main.js中全局引用
JavaScript代码
  1. // 防止多次点击  
  2. import preventReClick from '@/util/preventReClick '  
  3. Vue.use(preventReClick);  
3.在触发事件的按钮上就可以直接使用指令(button按钮和其他元素都可)
JavaScript代码
  1. <div class="comment-btn" @click="submitMes()" v-preventReClick="3000">发送</div>  
  2. <el-button @click="submitMes()" v-preventReClick="3000">发送</el-button>  

2. 防抖(debounce)

用户停下操作,就执行函数;只要不停止操作,永远不会执行函数内的操作
使用场景:防抖常应用于用户进行搜索输入节约请求资源,
eg:原来watch用户输入值改变马上请求接口 ,加入防抖,停止输入操作才会对接口进行访问

JavaScript代码
  1. /** 
  2.  * @description 函数防抖 触发高频时间后n秒内函数只会执行一次,如果n秒内高频时间再次触发,则重新计算时间。 
  3.  * @param {Function} func 需要执行的函数 
  4.  * @param {Number} wait 间隔时间 默认200ms 
  5.  * @param {Boolean} immediate  是否立即执行 true(默认) 表立即执行,false 表非立即执行 
  6.  * @return {*} 
  7.  */  
  8. export function VueDebounce(func, wait = 200, immediate = true) {  
  9.     let timeout = null;  // 定时器  
  10.     return function () {  
  11.         let that = this// this对象  
  12.             args = arguments; // 参数  
  13.         if (timeout) clearTimeout(timeout);  
  14.         if (immediate === true) { // 立即执行  
  15.             var callNow = !timeout;  
  16.             timeout = setTimeout(() => {  
  17.                 timeout = null;  
  18.             }, wait)  
  19.             if (callNow) {  
  20.                 // func.apply(that, args); // 普通用法  
  21.                 that[func](...args); // vue用法  
  22.             }  
  23.         }  
  24.         else { // 非立即执行  
  25.             timeout = setTimeout(() => {  
  26.                 // func.apply(this, args); // 普通用法  
  27.                 that[func](...args); // vue用法  
  28.             }, wait);  
  29.         }  
  30.     }  
  31. }  

用法:

JavaScript代码
  1. import {VueDebounce} from "@/util/index"  
  2. methods: {  
  3.     /** 
  4.      * 点击事件 函数防抖 
  5.      * 用法:<el-button @click="debounceHandel">点击测试</el-button> 
  6.      */  
  7.     debounceHandel: VueDebounce("handlerFunc"),  
  8.    
  9.     /** 
  10.      * 点击事件:真正执行的函数 
  11.      */  
  12.     handlerFunc(type) {  
  13.       console.log("测试防抖事件");  
  14.       this.$emit("click","这是参数"); // 如果用普通用法,则这里会找不到$emit,因为this还往上继承了vue的对象  
  15.     },  
  16.   }  
  17.    

3. 节流(throttle)

高频时间触发,但n秒内只会执行一次,所以节流会稀释函数的执行频率,是个固定的过程 ,如限制1s,则1s内只执行一次,无论怎样,都在会1s内执行相应的操作

使用场景:和防抖使用场景差不多,关键看固定时间内(1s)需要反馈,需要反馈使用节流,即:无论用户是否停下操作,都会固定时间执行函数操作

JavaScript代码
  1. * @description 函数节流  
  2. * @param {Function} func 函数  
  3. * @param {Number} wait 延迟执行毫秒数,默认200  
  4. * @param {Number} type 1 表时间戳版,2 表定时器版  
  5. */  
  6. xport function VueThrottle(func, wait=200 ,type) {  
  7.    if(type===1){  
  8.        let previous = 0;  
  9.    }else if(type===2){  
  10.        let timeout;  
  11.    }  
  12.    return function() {  
  13.        let that= this;  
  14.        let args = arguments;  
  15.        if(type===1){  
  16.            let now = Date.now();  
  17.   
  18.            if (now - previous > wait) {  
  19.                // func.apply(that, args); // 普通用法  
  20.                that[func](...args); // vue用法  
  21.                previous = now;  
  22.            }  
  23.        }else if(type===2){  
  24.            if (!timeout) {  
  25.                timeout = setTimeout(() => {  
  26.                    timeout = null;  
  27.                    // func.apply(that, args)  
  28.                    that[func](...args); // vue用法  
  29.                }, wait)  
  30.            }  
  31.        }  
  32.    }  

用法:

JavaScript代码
  1. import {VueThrottle} from "@/util/index"  
  2. methods: {  
  3.     /** 
  4.      * 点击事件 函数防抖 
  5.      * 用法:<el-button @click="throttleHandel">点击测试</el-button> 
  6.      */  
  7.     throttleHandel: VueThrottle("handlerFunc"),  
  8.    
  9.     /** 
  10.      * 点击事件:真正执行的函数 
  11.      */  
  12.     handlerFunc(type) {  
  13.       console.log("测试防抖事件");  
  14.       this.$emit("click","这是参数"); // 如果用普通用法,则这里会找不到$emit,因为this还往上继承了vue的对象  
  15.     },  
  16.   }  
  17.    
H5/JS/CSS | 评论(0) | 引用(0) | 阅读(423)