Vue2.x-使用防抖以及节流的示例
2021-03-05 04:06
627 查看
utils:
// 防抖 export const debounce = (func, wait = 3000, immediate = true) => { let timeout = null; return function() { let context = this; let args = arguments; if (timeout) clearTimeout(timeout); if (immediate) { var callNow = !timeout; //点击第一次为true 时间内点击第二次为false 时间结束则重复第一次 timeout = setTimeout(() => { timeout = null; }, wait); //定时器ID if (callNow) func.apply(context, args); } else { timeout = setTimeout(function() { func.apply(context, args); }, wait); } }; }; // 时间戳方案 export const throttleTime = (fn, wait = 2000) => { var pre = Date.now(); return function() { var context = this; var args = arguments; var now = Date.now(); if (now - pre >= wait) { fn.apply(context, args); pre = Date.now(); } }; }; // 定时器方案 export const throttleSetTimeout = (fn, wait = 3000) => { var timer = null; return function() { var context = this; var args = arguments; if (!timer) { timer = setTimeout(function() { fn.apply(context, args); timer = null; }, wait); } }; };
vue中使用:
<template> <div class="main"> <p>防抖立即执行</p> <button @click="click1">点击</button> <br /> <p>防抖不立即执行</p> <button @click="click2">点击</button> <br /> <p>节流时间戳方案</p> <button @click="click3">点击</button> <br /> <p>节流定时器方案</p> <button @click="click4">点击</button> </div> </template> <script> import { debounce, throttleTime, throttleSetTimeout } from './utils'; export default { methods: { click1: debounce( function() { console.log('防抖立即执行'); }, 2000, true ), click2: debounce( function() { console.log('防抖不立即执行'); }, 2000, false ), click3: throttleTime(function() { console.log('节流时间戳方案'); }), click4: throttleSetTimeout(function() { console.log('节流定时器方案'); }) }, }; </script> <style scoped lang="scss"> * { margin: 0; font-size: 20px; user-select: none; } .main { position: absolute; left: 50%; transform: translateX(-50%); width: 500px; } button { margin-bottom: 100px; } </style>
解释:
防抖:
立即执行版本:immediate为true,则点击第一次就执行,再继续点击则不执行,当wait时间结束后,再点击则生效,也就是只执行第一次。
原理:
点击第一次不存在timeoutID,并且callNow为true,则立即执行目标代码,点击第二次时存在了timeoutID,并且callNow为false,所以不执行目标代码,当wait时间结束后,把timeoutID设为null,则开始重复立即执行逻辑。
不立即执行版:immediate为false,则点击第一次不执行,当wait时间结束后,才生效,也就是无论点击多少次,只执行最后一次点击事件
原理:
使用setTimeout延迟执行事件,如果多次触发,则clearTimeout上次执行的代码,重新开始计时,在计时期间没有触发事件,则执行目标代码。
节流:
连续触发事件时以wait频率执行目标代码。
效果:
以上就是Vue2.x-使用防抖以及节流的示例的详细内容,更多关于vue 防抖及节流的资料请关注脚本之家其它相关文章!
您可能感兴趣的文章:相关文章推荐
- Javascript函数防抖与节流的理解以及在vue和原生js中的使用
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- 使用Log4j为项目配置日志输出应用详解以及示例演示的实现分析
- Ansible基本配置以及使用示例
- AutoCompleteExtender控件使用示例以及BUG
- Android线程池(一)——Executors(线程池)以及FutureTask使用示例
- Spring dbcp连接池配置与示例(以及JDBCTemplate的使用)
- 【swing】JLabel详解以及使用示例
- 演示事件(Event)怎样使用以及怎样为用户控件添加一个事件(示例代码下载)
- SenchaTouch2.3.1 中使用listpaging以及pullrefresh插件 做的分页示例
- tinyxml的使用以及示例
- 由一个简单示例 引出java继承中父子类成员变量共享问题 以及super的使用
- 普元NUI使用参考文档以及示例
- cocos2dx 3.15 视频播放实现 lua 以及c++ 版本VideoPlayer的使用注意 与详细示例
- 关于Arduino 使用软pwm的方法以及与上位机的通讯(带示例)
- Markdown使用以及示例
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- Clucene的编译安装以及使用示例
- 模板引擎smarty工作原理以及使用示例
- 支持xcode6的缓动函数Easing以及使用示例