点击事件单双击判断
判断是否是双击事件,以及分别执行单双击事件
function isDoubleClick(singleCallback, doubleCallback, delay = 300) {
this.clickEvent = '';
this.firstClickTime = '';
this.clickNum = 0;
return (event) => {
const time = new Date().getTime();
this.clickNum++;
if ((time - this.firstClickTime <= delay) && this.clickEvent === event.target.innerHTML) {
this.clickNum = 0;
doubleCallback.call(this, event)
} else {
this.clickEvent = event.target.innerHTML;
this.firstClickTime = time;
const timeOut = setTimeout(() => {
if (this.clickNum % 2 !== 0) {
singleCallback.call(this, event)
this.clickEvent = '';
this.firstClickTime = '';
this.clickNum = 0;
}
clearTimeout(timeOut);
}, [delay])
}
}
}
function add(a, b) {
console.log(a + b);
}
document.addEventListener('click', isDoubleClick((arguments) => {
// console.log(arguments);//根据触发点选择执行事件
// add(1, 2);//执行事件
console.log('你正在单击' + arguments.target.innerHTML + "按钮");
}, function (arguments) {
// console.log('这是双击事件', arguments);
console.log('你正在双击' + arguments.target.innerHTML + "按钮");
}, 300));
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 从安的博客!
评论