我使用JavaScript实现扫雷游戏。我需要使用mousedown处理程序处理3个事件(按向左按钮,按向右按钮,同时按向左和向右)。
let isLeftMouseDown = false;
let isRightMouseDown = false;
let timer,
timeout = 100;
function clickCell(event){
let $target = event.target;
let clickCell = getCellById($target.id);
let isRightMB;
let isLeftMB;
event = event || window.event;
if ("which" in event){
isRightMB = event.which == 3;
isLeftMB = event.which == 1;
}
else if ("button" in event){
isRightMB = e.button == 2;
isLeftMB = e.button == 0;
}
if(isLeftMB){
isLeftMouseDown = true;
}
else if(isRightMB){
isRightMouseDown = true;
}
if(!timer){
timer=setTimeout(function(){
if(isLeftMouseDown){
if(!clickCell.flag){
$($target).removeClass("close").addClass("open0");
}
}
else if(isRightMouseDown){
toggleFlag(clickCell)
}
}, timeout)
}
else{
clearTimeout(timer);
timer = null;
if(isLeftMouseDown && isRightMouseDown){
openCellEnviroment($target);
}
}
}
$field.on('mousedown', clickCell);
我使用了setTimeOut
,但效果并不理想。我想知道其他方式。