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

120
Visualizações
Submit form delay with mousedown with cancel on mouseup

Im trying to create a form that will submit with a long press (currently 2 seconds) but being able to cancel (or prevent submitting the form) when the user stops holding the submit button to give the user time to change their mind about submitting the form.

function conf_submit(btn) {

$(btn).mousedown(function() {
btn_timeout = setTimeout(function() {
  alert("form submitted");
}, 2000);
$(this).val("Release to cancel!");
$(this).attr('class', 'btn_down');
});

$(btn).mouseup(function() {
clearTimeout(btn_timeout);
$(this).val("Submit");
$(this).attr('class', 'btn_up');
});
}

FIDDLE

The button is suppose to change text, i wanted to have a count down after 'Release to cancel!' so the user knows how long until the form submits but couldn't get this to work.

My problem is; The first time you click the link it doesnt fire, it also continues to fire even on mouseup.

Thanks in advance for any support.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Just call conf_submit once with the button element when page loads, not each time the button is clicked.

function conf_submit(btn) {

  $(btn).mousedown(function() {
    btn_timeout = setTimeout(function() {
      alert("form submitted");
    }, 2000);
    $(this).val("Release to cancel!");
    $(this).attr('class', 'btn_down');
  });

  $(btn).mouseup(function() {
    clearTimeout(btn_timeout);
    $(this).val("Submit");
    $(this).attr('class', 'btn_up');
  });
}

[...document.querySelectorAll(".btn_longpress")].forEach(conf_submit);
.btn_down {
  background-color: red;
}

.btn_up {
  background-color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <input class="btn_up btn_longpress" type="button" value="Submit" />
  <input class="btn_up btn_longpress" type="button" value="Submit2" />
</div>

about 4 years ago · Juan Pablo Isaza 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