1. 程式人生 > >雙擊事件失效解決辦法

雙擊事件失效解決辦法

dev out == pre clear win settime prev gettime

某些時候,雙擊事件會失效,模擬辦法解決雙擊失效後,而同時我又不想在該元素上觸發兩次單擊事件,解決辦法如下:

var dbclickTime={
prev:0, next:0//模擬觸發雙擊
};
var clickConflict={//解決單擊事件和雙擊事件的沖突
_timeout:null,
set:function (fn) {
this.clear();
this._timeout=window.setTimeout(fn,400);
},
clear:function () {
if(this._timeout){
window.clearTimeout(this._timeout);
this._timeout=0;
}
}
};

//code 事件托管
document.addEventListener("click",function (e) {
if(dbclickTime.prev==0){
dbclickTime.prev=dbclickTime.next=new Date().getTime();
}else{
dbclickTime.prev=dbclickTime.next;
dbclickTime.next=new Date().getTime()
}

if(true){//托管的元素
clickConflict.set(function () {
          console.log("click");
          //do something
        });
}

if(dbclickTime.next-dbclickTime.prev<300){
console.log("dblick");
clickConflict.clear();
if(true){//托管的元素
//do something
}
}
},true);

雙擊事件失效解決辦法