如何在JavaScript中连续增加或减少值

当鼠标单击并按住元素加号时,我想增大或减小值,它正在工作,但在鼠标保持上不起作用

var salainv = document.getElementById("sala").value;
var bodegainv = document.getElementById("bodega").value;

function incrementar() {
  document.getElementById("sala").value = salainv++;
  document.getElementById("bodega").value = bodegainv--;
}

function decrementar() {
  document.getElementById("sala").value = salainv--;
  document.getElementById("bodega").value = bodegainv++;
}
<input type="number" name="bodega" id="bodega" value="15">
<input type="number" name="sala" id="sala" value="5">
<img src="https://cdn.pixabay.com/photo/2012/04/13/00/21/plus-31216_960_720.png" style="width: 40px;" onClick="incrementar()">
<img src="http://images.clipartpanda.com/minus-clipart-RTA7dagTL.png" style="width: 40px;" onClick="decrementar()">

有人可以帮我按鼠标吗?

谢谢.

解决方法:

您可以将如下所示的setInterval与onMouseDown和onMouseUp事件一起使用

var salainv = document.getElementById("sala").value;
var bodegainv   = document.getElementById("bodega").value;

var timerId;
function incrementar() {
      document.getElementById("sala").value = salainv++;    
      document.getElementById("bodega").value = bodegainv--; 
 }
 function beginIncrementar(){
     timerId = setInterval(incrementar,200);
 }
 function endIncrementar(){
     clearInterval(timerId)
 }
 function decrementar() {
      document.getElementById("sala").value = salainv--;
      document.getElementById("bodega").value = bodegainv++; 
 }
 
 function beginDecrementar(){
     timerId = setInterval(decrementar,200);
 }
 function endDecrementar(){
     clearInterval(timerId)
 }
<input type="number" name="bodega" id="bodega" value="15">
<input type="number" name="sala" id="sala" value="5">
<img src="https://cdn.pixabay.com/photo/2012/04/13/00/21/plus-31216_960_720.png" style="width: 40px;" onClick="incrementar()" onm ouseDown="beginIncrementar()" onm ouseUp="endIncrementar()">
<img src="http://images.clipartpanda.com/minus-clipart-RTA7dagTL.png" style="width: 40px;" onClick="decrementar()" onm ouseDown="beginDecrementar()" onm ouseUp="endDecrementar()">

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。