2

I got this code from: Jquery: mousedown effect (while left click is held down) It is behaving that if I am holding the button in every 50ms it will do something.

var timeout = 0;
$('#button_add').mousedown(function () {
    timeout = setInterval(function () {
        value_of_something ++;
    }, 50);
    return false;
    });
});

But what I want is to execute this part after holding down the button by 1 second and it will continuously do the action 50ms.

Community
  • 1
  • 1
keisaac
  • 336
  • 3
  • 15

2 Answers2

3

As I said you need to use setTimeout()

var timeout = 0;
$('#button_add').mousedown(function () {
    setTimeout(function(){
      timeout = setInterval(function () {
                   value_of_something ++;
                }, 50);         
    } , 1000);
    return false;
});

Jsfiddle

Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
0

Please use this code... You have to clearTimeout when user mouseup :

var timeout = 0;

$('#button_add').mousedown(function() {
    oneSecondTimer = setTimeout(function() {
        timeout = setInterval(function() {
            value_of_something++;
        }, 50);
    }, 1000);

    return false;
});

$("#button_add").mouseup(function() {
    clearTimeout(oneSecondTimer);
});