在单击事件中将绑定的函数放在定时器中,利用定时器的延迟来清除双击对单击事件的影响,如果是双击事件则在双击事件内部清除定时器,如果是单击事件则正常执行定时器中的函数;但是需要记得每次进入单击事件都需要清除定时器。
1、定义外部定时器变量。
2、定义单击事件。
3、定义双击事件。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
</head>
<body>
<button id="div1">clickme</button>
<script type="text/javascript">
var clickTimer='';
document.getElementById('div1').ondblclick=function(){
clearInterval(clickTimer);
alert('123');
}
document.getElementById('div1').onclick=function(){
clearInterval(clickTimer);
clickTimer=setInterval(function(){
console.log('123');
},300);
}
</script>
</body>
</html>