Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

69
Vistas
Show contextmenu only after long press

I want to activate a context-menu only after left-mousedown is pressed for N seconds (e.g. 2 seconds).
If the mouse is released, the context-menu should not be shown.
I tried to use:

  • setTimeout and clearTimeout - to handle the wait time.
  • jquery contextmenu - for using the context-menu.

But I cannot get the expected behaviour.
What I get is a delay before showing the context-menu for the first time, but:

  • if I stop pressing the mouse before the wait time, the context-menu still shows up.
  • after the first time, the context-menu is shown with every click, even is the mouse is released before the wait time.

How can I condition showing the context-menu, only after the mouse is clicked for a specified time period?

Thanks

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

You can reach it with this solution

const MouseDownDuration = class {
    setMouseDown() {
        this.downAt = new Date();
    }

    setMouseUp() {
        this.upAt = new Date();
    }

    get duration () {
        return this.upAt.getTime() - this.downAt.getTime()
    }
}

const contextMenu = new MouseDownDuration();


document.querySelector('#element').addEventListener('mousedown', (e) => {
    contextMenu.setMouseDown();
});

document.querySelector('#element').addEventListener('mouseup', (e) => {
    contextMenu.setMouseUp();

    // Log the diff in milliseconds
    console.log(contextMenu.duration);

    // Eventually dispatch a custom event
});

You can also see how to dispatch an event using e.target element by checking https://developer.mozilla.org/en-US/docs/Web/Events/Creating_and_triggering_events

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda