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?
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.
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.
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.
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.
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>
You'll need 3 event handler:
mousedown
that will call a function, that will call itself with a timeout (continuosIncerment
) while the mouse button is pressed.mouseup
that will clear the timeout when the button is released.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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With