6

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?

Angel Politis
  • 10,955
  • 14
  • 48
  • 66
user1022585
  • 13,061
  • 21
  • 55
  • 75
  • There are the `onmousedown` and `onmouseup` event handlers. I'm sure you can do something with that: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onmousedown Perhaps also look at `onmouseout`. – KIKO Software Jan 25 '18 at 11:53

4 Answers4

8

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>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
5

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>
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
3

Instead of reading the value from the HTML, then writing it back, it's easier to hold the value in a variable, increment it, then write it out.

Did you know you can do this with a simple HTML spinner?

<input type="number" min="0" max="50" step="1">
DJDaveMark
  • 2,669
  • 23
  • 35
0

I'd go with a solution like this: on mouse down event starts a repeating timer that triggers your function and it stops when the mouse up event occurs.

var inter = null;

function setInter(){   
    inter=setInterval(incrementValue, 500);
}

function unsetInter(){
    clearInterval(inter);
}

function incrementValue() {
   var value = parseInt(document.getElementById('skill_1').innerHTML);
   value = value + 1;
   document.getElementById('skill_1').innerHTML = value;
}
<button 
onmousedown="setInter()"
onmouseup="unsetInter()"> add </button>
<br>
<span id=skill_1>0</span>
Cirou
  • 1,420
  • 12
  • 18