Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make it work only with left mouse down

Tags:

javascript

Holla ,

this is the javascript im using

var timeoutID;

    function setup() {


            document.getElementById("demo").addEventListener("mousedown", resetTimer);


        startTimer();
    }
    setup();

    function startTimer() {

        timeoutID = window.setTimeout(goInactive, 1000);
    }

    function resetTimer() {
        window.clearTimeout(timeoutID);
        goActive();
    }

    function goInactive() {
        document.getElementById("demo").style.opacity = 0;
        document.getElementById("demo").style.transition = "1s";
    }

    function goActive() {
        document.getElementById("demo").style.opacity = 1;
        document.getElementById("demo").style.transition = "1s";

        startTimer();
    }   

everything is working fine , except i want it to go active only by left mouse button ( now right click left click and middle click are doing the job )

So many thanks in advance AliYous


1 Answers

You have to detect the left mouse button press by checking the event object (Detect left mouse button press).

evt = evt || window.event;
if ("buttons" in evt) {
    return evt.buttons == 1;
}

And then if it's left mouse (1), proceed to do your job in resetTimer() function. Final code will be:

function resetTimer(e) {
    evt = e || window.event;
    if ("buttons" in evt) {
        if (evt.buttons == 1) {
            window.clearTimeout(timeoutID);
            goActive();
        }
    }
}
like image 101
Praveen Kumar Purushothaman Avatar answered Sep 10 '25 04:09

Praveen Kumar Purushothaman