if button is keeping pressed execute increment javascript

Solutions on MaxInterview for if button is keeping pressed execute increment javascript by the best coders in the world

showing results for - "if button is keeping pressed execute increment javascript"
César
01 Mar 2018
1var timeout;
2var speed = 500;
3
4// Increment button
5$('#plus-btn').on('mousedown mouseup mouseleave', e => {
6  if (e.type == "mousedown") {
7    increment(speed);
8  } else {
9    stop()
10  }
11});
12
13// Increment function
14function increment(speed) {
15  $('#qty-input').val(parseInt($('#qty-input').val()) + 1);
16  timeout = setTimeout(() => {
17    increment(speed * 0.8);
18  }, speed);
19}
20
21function stop() {
22  clearTimeout(timeout);
23}