我使用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
,但它并不完美。我想知道还有其他的方法。