初学自己写的小案例,介意的请看别人封装好的!!!喷子勿扰!!!
核心js:
export function debounce(func,isClick,obj,mess){
let timer = null;
return function(){
isClick?func():obj.$message({type:"info",message:mess});
obj.isClick= false;
if(timer)clearTimeout(timer)
timer = setTimeout(()=>{
obj.isClick = true
},5000)
}()
}
应用:
<template>
<div class="count">
<span class="add" @click="reduceOrAdd('-')">-</span>
<span>{{pageIndex}}</span>
<span class="add" @click="reduceOrAdd('+')">+</span>
</div>
</template>
<script>
export default {
name:"Count",
data(){
return {
pageIndex:0,
isClick:true,
timer:null
}
},
methods:{
reduceOrAdd(option){
if(option=="-"){
debounce(this.reduce,this.isClick,this,"加载中...")
} else {
debounce(this.add,this.isClick,this,"加载中...")
}
},
add(){
this.pageIndex++;
},
reduce(){
if(this.pageIndex==0)return this.pageIndex;
}
}
}
</script>
<style scoped>
.count{
width:100px;
display: flex;
justify-content: space-around;
align-items:center;
}
.add{
display: inline-block;
width:20px;
height:20px;
border:1px solid gray;
text-align: center;
line-height:20px;
font-size:20px;
}
</style>
待解决问题:
- 设置修改isClick的延迟为1s,点击一次按钮后如果继续狂点,就算据第一次点击过了10s,持续不断的点也会导致锁定变量isClick永远为false。
- 期望:点击第一次以后,无论中间点多少次,只要据第一次点击间隔为1s,锁定变量isClick为true。