Identify a long click with jquery

2

Galera I mounted a context menu that works like this, when the user right-clicks on a 'tr' table it opens.

I do this by using jQuery:

// Verifica se abre o menu
$("tr").mousedown(function (e) {
  //AQUI FICA AS FUNÇÕES DO MENU
});

Well, the problem is that when it's opened on a mobile phone, you do not have the mouse.

I wanted to know if you have any way to identify a click for 2 seconds, and open the menu.

Then it should open either with the right button or a long click.

    
asked by anonymous 15.12.2016 / 13:41

1 answer

3

Or you can use the jQuery Mobile API's taphold event:

  

The jQuery Mobile taphold event triggers after a touch event   complete and sustained (also known as long pressure).

For your case it would look more or less:

$("tr").on( "taphold", function( event ) {
  //AQUI FICA AS FUNÇÕES DO MENU
});

With jQuery only, you can calculate the time between mousedown and mouseup :

var tempo;

$("div").mouseup(function(){
  
  // diferença entre clicar e soltar
  var total = new Date().getTime() - tempo;
  var segundos = ((total % 60000) / 1000).toFixed(0);
  console.log(total); // total se millis
  console.log(segundos); // total em segundos
  
  if (segundos > 0){
    console.log("Oi");
  }
  
}).mousedown(function(){
  
  // set tempo ao clicar
  tempo = new Date().getTime();
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  Teste
</div>
    
15.12.2016 / 13:50