Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript increment value faster with mouse hold [duplicate]

I have this script that adds 1 to a value every time I click on a button:

<script>
    function incrementValue(id) {
       var value = parseInt(document.getElementById(id).innerHTML);
       value = value + 1;
       document.getElementById(id).innerHTML = value;
    }
</script>

<button onclick="incrementValue('skill_1')"> add </button><br>
<span id=skill_1>0</span>

However I want to adjust it so that if I hold down the mouse button, it'll repeat so I don't have to keep pressing it over and over.

Any way to do that using javascript? Or would jquery suit?

like image 576
user1022585 Avatar asked Jan 25 '18 11:01

user1022585


People also ask

How to increment a variable by 1 in JavaScript?

In JavaScript, we can increment a variable by 1 easily by using the increment operator (++). Let’s see a simple example of this below. In the example below, we will have an array of numbers that we will want to get the total value of.

How to use ++incremental operator in JavaScript?

Incremental operator ++ used to increase the existing variable value by 1 (x = x + 1). Increment operators in JS programming are used in For Loop, While loop, and Do While loops. ++i (Pre increment): It will increment the value of i even before assigning it to the variable i.

How to increment a value twice when you run a function?

Then we define function f that increments count by 1. Then we call f twice. As a result, count is 2 according to the console log. To increment value each time when you run function with JavaScript, we can create a variable outside the function and increment that when the function is run.

What is I++ (post-increment) in JavaScript?

i++ (Post-increment): The operator returns the variable value first (i.e, i value) then only i value will incremented by 1. Here is the HTML example code of auto-increment x and y variables in JS.


2 Answers

To achieve this you need to use the mousedown event to start a timeout (which is the delay before the incremental count starts) and an interval (which does the repeated counting). You'll also need a mouseup and mouseleave handler to remove both of those timers. Try this:

var timeout, interval;

[].forEach.call(document.querySelectorAll('.add'), function(button) {
  button.addEventListener('mousedown', function() {
    var id = button.dataset.target;
    incrementValue(id);
    
    timeout = setTimeout(function() {
      interval = setInterval(function() {
        incrementValue(id);
      }, 50);    
    }, 300);
  });
  
  button.addEventListener('mouseup', clearTimers);
  button.addEventListener('mouseleave', clearTimers); 
  
  function clearTimers() {
    clearTimeout(timeout);
    clearInterval(interval);
  }
});

function incrementValue(id) {
  var el = document.getElementById(id);
  var value = parseInt(el.textContent, 10);
  document.getElementById(id).textContent = ++value;
}
<button class="add" data-target="skill_1">add</button><br />
<span id="skill_1">0</span>
like image 162
Rory McCrossan Avatar answered Nov 13 '22 22:11

Rory McCrossan


You'll need 3 event handler:

  1. mousedown that will call a function, that will call itself with a timeout (continuosIncerment) while the mouse button is pressed.
  2. mouseup that will clear the timeout when the button is released.
  3. mouseleave that clears the timeout when the mouse leaves the button area.

const btn = document.querySelector('#btn');
const skill_1 = document.querySelector('#skill_1');
let value = 0;
let timer;

function continuosIncerment() {
  skill_1.innerHTML = ++value;
  
  timer = setTimeout(continuosIncerment, 200);
}

function timeoutClear() {
  clearTimeout(timer);
}

btn.addEventListener('mousedown', continuosIncerment);

btn.addEventListener('mouseup', timeoutClear);

btn.addEventListener('mouseleave', timeoutClear);
<button id="btn"> add </button><br>
<span id="skill_1">0</span>
like image 35
Ori Drori Avatar answered Nov 13 '22 21:11

Ori Drori