Randy Mayer Randy Mayer - 5 months ago 27
Javascript Question

Long Press in JavaScript?

Is it possible to implement "long press" in JavaScript (or jQuery)? How?

alt text

HTML

<a href="" title="">Long press</a>


JavaScript

$("a").mouseup(function(){
// Clear timeout
return false;
}).mousedown(function(){
// Set timeout
return false;
});

Answer

There is no 'jQuery' magic, just JavaScript timers.

var pressTimer;

$("a").mouseup(function(){
  clearTimeout(pressTimer);
  // Clear timeout
  return false;
}).mousedown(function(){
  // Set timeout
  pressTimer = window.setTimeout(function() { ... Your Code ...},1000);
  return false; 
});