Is there an event for sustained click on Javascript?

0

Do you know of an event in javascript that detects when you click and keeps clicking ?, You will see, I want to perform an action infinitely many times while you are clicking "maintained-sustained" to an HTML element, or have some idea How do you do this? Thank you very much in advance.

    
asked by juan gabriel gabo mogollon mar 17.06.2018 в 02:26
source

1 answer

2

See this code.

The first function is executed while the button is pressed.

The second one is executed when the click is lifted. Notice that it calculates the time elapsed between the click and its survey, by a combination of onmousedown and onmouseup .

I think with this you can do what you want.

var btnClick = document.getElementById('btnClick');
var startTime, endTime;

/*Cuando se haga clic*/
btnClick.onmousedown = function() {
  startTime = new Date();
  console.log("Estoy presionado, haz lo que necesites...");
};

/*Cuando se deje de hacer clic*/
btnClick.onmouseup = function() {
  endTime = new Date();
  var timeDiff = endTime - startTime; //en ms
  console.log("Se hizo clic:\n" + startTime);
  console.log("Se levantó el clic:\n" + endTime);
  console.log("Tiempo transcurrido:\n" + timeDiff + " ms");
};
<button id="btnClick">Haz Clic</button>
    
answered by 17.06.2018 в 03:20