按下左键,按下右键,使用mousedown处理程序同时按下左右按钮。

3

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

0

寻找这些

element.addEventListener('click', (e) => {//do something on click on element})

我只需要mousedown事件,因为在mouseup事件中使用其他处理程序。 - Tony

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接