The easiest approach to this is to start a cycle at a certain interval while one of the buttons is pressed, making changes to the values ββat each tick. Start when the button is pressed, stop when it is released. Here's a simplified code for demonstrating purpose only:
// Store the reference var range = $('#range'); // These functions will change the value function increment () { range.val(parseInt(range.val()) + 1) } function decrement () { range.val(parseInt(range.val()) - 1) } // Attaches polling function to element function poll (el, interval, fn) { var isHeldDown = false; return el .on("mousedown", function() { isHeldDown = true; (function loop (range) { if (!isHeldDown) return; // Stop if it was released fn(); setTimeout(loop, interval); // Run the function again })(); }) .on("mouseup mouseleave", function () { isHeldDown = false; // Stop polling on leave or key release }); } poll($("#plus"), 40, increment); poll($("#minus"), 40, decrement);
JSFiddle .
In the production class version, you want to use the synchronization function instead of a constant interval; think about all the possible events that should start and stop the survey so that it does not get stuck forever when the user moves the pointer or something else; use requestAnimationFrame for more precise timing control.
source share