Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

68
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda