He aquí una solución que es compatible con los clics y sostiene:
// Timeout, started on mousedown, triggers the beginning of a hold
var holdStarter = null;
// Milliseconds to wait before recognizing a hold
var holdDelay = 500;
// Indicates the user is currently holding the mouse down
var holdActive = false;
// MouseDown
function onMouseDown(){
// Do not take any immediate action - just set the holdStarter
// to wait for the predetermined delay, and then begin a hold
holdStarter = setTimeout(function() {
holdStarter = null;
holdActive = true;
// begin hold-only operation here, if desired
}, holdDelay);
}
// MouseUp
function onMouseUp(){
// If the mouse is released immediately (i.e., a click), before the
// holdStarter runs, then cancel the holdStarter and do the click
if (holdStarter) {
clearTimeout(holdStarter);
// run click-only operation here
}
// Otherwise, if the mouse was being held, end the hold
else if (holdActive) {
holdActive = false;
// end hold-only operation here, if desired
}
}
// Optional add-on: if mouse moves out, then release hold
function onMouseOut(){
onMouseUp();
}
He aquí una demostración: http://jsfiddle.net/M7hT8/1/
basada originalmente en la solución de daveyfaherty. Sé que esta pregunta es de hace un tiempo, pero estoy compartiendo mi solución para cualquiera que encuentre esto a través de una búsqueda.
@MrOBrian parámetros de variables globales en mousedown y mouseup de verdadero/falso, a continuación, comprobar en el método de hacer clic para una variable, pero siempre se dispara mouseup primera – aperture
se podría tratar de usar un temporizador en ratón que va a continuación, establecer una variable o enlaza el evento correcto para saber si el botón se mantuvo presionado durante al menos una cierta cantidad de tiempo. – MrOBrian