Check if mousedown with an if statement?

前端 未结 5 644
悲哀的现实
悲哀的现实 2021-02-02 17:16

Is it possible to do something like this:

if ($(this).mousedown() == true) {

I thought that would work but it doesn\'t.

Additio

5条回答
  •  感情败类
    2021-02-02 17:55

    Check out this one. http://jsfiddle.net/b1Lzo60n/

    Mousedown starts a timer that checks if mouse is still down after 1 second.

    
    

    Code:

    var mousedown = false;
    var mousedown_timer = '';
    $('#button').mousedown(function(e) {
        mousedown = true;
        $('#log').text('mousedown...');
        mousedown_timer = setTimeout(function () {
            if(mousedown) {
                $('#log').text('1 second');
            }
        }, 1000);
    }).mouseup(function(e) {
        mousedown = false;
        clearTimeout(mousedown_timer);
        $('#log').text('aborted');
    });
    

提交回复
热议问题