需求:点击计数器±,修改数据并提交
问题:由于计数器按钮可以长按,change监听到数据变化导致数秒内提交几十次数据
解决:element计数器有默认的change事件,所以我在change事件里添加mouseup事件提交数据,
问题2:change事件里的mouseup会执行多次累加(console出来的是1 1,2 1,2,3 1,2,3,4 …以此类推),也就是说还是会提交多次,而且越来越多
解决2:在on前面加一个off
handleChange(id, val) {
let _this = this;
$('.el-input-number span').off('mouseup').on('mouseup',function () {
_this.numChange(id,val)//提交数据函数
});
},
问题3:快速点击还是会提交多次
解决3:用定时器
handleChange(id, val) {
let _this = this;
$('.el-input-number span').off('mouseup').on('mouseup',function () {
_this.mytime=new Date().getTime()
clearTimeout(_this.mytimeout)
_this.mytimeout=setTimeout(function () {
if(new Date().getTime()-_this.mytime>500){
_this.numChange(id,val)//提交数据函数
}
},600)
});
}
问题4:到达最大最小数值限制时还会触发提交
解决4:下方代码
handleChange(id, val) {
let _this = this;
$('.el-input-number span').off('mouseup').on('mouseup',function () {
_this.mytime=new Date().getTime()
clearTimeout(_this.mytimeout)
_this.mytimeout=setTimeout(function () {
if(new Date().getTime()-_this.mytime>500){
if(val!=_this.min_max){
_this.numChange(id,val)//提交数据函数
_this.min_max=val
}
}
},600)
});
}