vue以组件或者插件的形式实现throttle或者debounce
作者:虚光 发布时间:2024-05-09 15:23:55
标签:vue,throttle,debounce
需求
在业务中,会碰到许多点击请求的情况,在请求前改变一个lock变量(在lock变回来之前,点击无效),在请求回调中再改变lock.以确保在,请求回来之前,不会重复请求。或者类似的点击节流业务
实现方式
指令
<div v-for="a in 3" :key="a" v-demo:getData="a">指令</div>
//getData是函数名,a是传入的参数
directives: {
demo: {
bind(el: Element, binding: any, vnode: VNode) {
const that: any = vnode.context
// console.log(binding, vnode)
// console.log(binding.arg, binding.value)
if (!that[binding.arg].isBind){ // 打上标记,如果已经转换了,就不转了
that[binding.arg] = deb(that[binding.arg])
that[binding.arg].isBind = true
}
el.addEventListener('click', function T(event: Event): void{
that[binding.arg](binding.value)
})
},
},
},
组件
子组件
<template>
<div>
<div @click="senClick">
<slot></slot>
</div>
</div>
</template>
<script lang="ts">
import Vue from 'vue';
import { Component, Prop } from 'vue-property-decorator';
@Component({})
export default class Child extends Vue {
@Prop({ type: Number, default: 500 }) public timeOut!: number; // 时间
@Prop({ type: String, default: 'throttle' }) public type!: string; // 类型
@Prop() public params!: any; // 传入参数
public message: string = 'Hello';
public throttleLock: boolean = false;
public debounceLock: number = 0;
public time: any;
public senClick(): void {
console.log(this.timeOut, this.type, this.params);
if (this.type === 'throttle') {
if (this.throttleLock) {
return;
}
this.throttleLock = true;
setTimeout(() => {
this.throttleLock = false;
}, this.timeOut);
this.$emit('myClick', this.params);
} else if (this.type === 'debounce') {
if (this.debounceLock) {
clearTimeout(this.debounceLock);
}
this.debounceLock = setTimeout(() => {
this.$emit('myClick', this.params);
}, this.timeOut);
} else {
this.$emit('myClick', this.params);
}
}
}
</script>
<style scoped lang='stylus'>
div {
width: 100%;
height: 100%;
}
</style>
父组件
<template>
<div class="home">
<throttle-and-debounce @myClick="getData" :time="500" type="throttle" params="123">
<div>我是组件内容</div>
</throttle-and-debounce>
</div>
</template>
import { Component, Vue } from 'vue-property-decorator';
import throttleAndDebounce from '@/components/throttleAndDebounce.vue';
@Component({
components: {
throttleAndDebounce,
},
})
export default class home extends Vue {
public getData(e: any){
console.log('异步数据', e)
}
}
</script>
plugin
函数
function deb(fn: function){
let lock: number
return (e) => {
if (lock){
clearTimeout(lock)
}
console.log('创建闭包延迟执行')
lock = setTimeout(() => {
fn(e)
}, 1500)
}
}
export {deb}
组件内使用
<template>
<div class="home">
<div @click="func(123)">function</div>
</div>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import {deb} from '@/assets'
@Component({
components: {
throttleAndDebounce,
},
})
export default class home extends Vue {
public beforeCreate(){
this.func = deb((e: {a: number}) => {
console.log('this', e)
})
}
}
</script>
来源:https://juejin.im/post/5c18a7b7f265da61553ac179


猜你喜欢
- 什么是insert buffer? 插入缓冲,也称之为insert buffer,它是innodb存储引擎的关键特性
- 本文实例为大家分享了OpenCV实现图片亮度增强或减弱的具体代码,供大家参考,具体内容如下对每个像素点的三通道值进行同步放大,同时保持通道值
- golang作为一热门的兼顾性能 效率的热门语言,相信很多人都知道,在编程语言排行榜上一直都是很亮眼,作为一门强类型语言,二进制位的操作肯定
- 如下所示:beta分布的最大特点是其多样性, 从下图可以看出, beta分布具有各种形态, 有U形, 类似正态分布的形状, 类似unifor
- python2:print语句,语句就意味着可以直接跟要打印的东西,如果后面接的是一个元组对象,直接打印python3:print函数,函数
- 1. SELECT INTO 语句用途:SELECT INTO 语句从一个表复制数据,然后把数据插入到另一个新表中,表结构与查询结构一致。P
- 索引和切片是NumPy中最重要最常用的操作。熟练使用NumPy切片操作是数据处理和机器学习的前提,所以一定要掌握好。文档:https://d
- 引言今天我们来分享一个 Python 领域的神级第三方库 -- pycallgraph,通过该库并结合 graphviz 工具,就可以非常方
- 一旦获得MySQL服务器的连接,需要选择一个特定的数据库工作。这是因为MySQL服务器可能有一个以上的数据库。从命令提示符,选择MySQL数
- 本文介绍了prototype.js常用函数及其使用方法例子说明函数名
- JavaScript获取最近7天日期可以使用 JavaScript 中的 Date() 对象和数组方法来获取最近7天的日期。以下是一种获取最
- 爬虫利器BeautifulSoup中find和find_all的使用方法二话不说,先上段HTML例子<html> &
- 这段时间微信跳一跳这个游戏非常火爆,但是上分又非常的难,对于程序员来说第一个念头就是通过写一个辅助脚本 * 让上分变的容易,python现在比
- 今天开发时,使用axios返回的response中data有多个数据:如果是获取cn里的数据的,可以用:response.data.cn但是
- Python中的五种特性:切片,迭代,列表生成式,生成器,迭代器。切片切片就相当于其他语言中的截断函数,取部分指定元素用的。L = list
- <script language="javascript" src="js/sett
- 这里使用TensorFlow实现一个简单的卷积神经网络,使用的是MNIST数据集。网络结构为:数据输入层–卷积层1–池化层1–卷积层2–池化
- 概述Rollup, 和 Webpack, Parcel 都是模块打包工具(module bundler tool), 但是侧重点不同, 我们
- 在微信开发中,写过的一个简单的音乐播放组件,记录下。music音乐播放组件。属性属性名类型默认值说明musicString 传入的
- 在公司做分布式深网爬虫,搭建了一套稳定的代理池服务,为上千个爬虫提供有效的代理,保证各个爬虫拿到的都是对应网站有效的 * ,从而保证爬虫快