3

I'm trying to add adelay on my submit button in javascript. But it just seems to freeze and no longer commits any action after clicking on it. Does anyone have an explanation ?

function Progress() {
   var button = document.getElementById("Button");
   var form = document.getElementById("new_video")

    form.onsubmit = function() {
      return false;
    }
    button.onclick = function() {
     setTimeout(function() {
        form.submit();
     }, 2000);
       return false;
    }
  }
<input type="submit" name="commit" value="Upload" class="btn btn btn-primary" onclick="Progress()" id="Button" data-disable-with="Upload" disabled="">
3
  • <input .... disabled=""> How do you click on a disabled button? Is that property removed? Commented Jul 7, 2020 at 21:15
  • 1
    why are you using type=submit instead of type=button if you don't want it to submit the form? i see code in here to submit the form but then a return false when it does and i'm a little confused Commented Jul 7, 2020 at 21:17
  • If you don't need to maintain Enter key support, I suggest switching to type="button". It solves your problem nicely Commented Jul 7, 2020 at 21:30

2 Answers 2

4

// Cache your Form Elements
const EL_form = document.querySelector("#myForm");
const EL_formSubmitBtn = EL_form.querySelector("#Button");

const Progress = (evt) => {
  evt.preventDefault(); // Prevent Browser Submit action
  
  EL_formSubmitBtn.disabled = true; // Disable the submit button
  
  setTimeout(function() {
    EL_form.submit(); // Or do AJAX stuff here
    EL_formSubmitBtn.disabled = false; // Enable the submit button
  }, 2000);
};


// Use Element.addEventListener instead of inline JS
// Don't attach handlers to button, rather use the Form "submit" Event:
EL_form.addEventListener("submit", Progress);
<form id="myForm" action="demo.php">
  <input id="Button" type="submit" value="Upload" class="btn btn btn-primary">
</form>

PS: Don't capitalise names of regular functions that do not actually return an Object - or are not a Class by definition, use rather progress as your function name. Or rather be more descriptive, it's actually a formSubmitHandler

Sign up to request clarification or add additional context in comments.

2 Comments

I like this answer because it stops the user from spamming the enter key or double clicking submit while they impatiently wait for the ajax call.
Thanks for the valuable feedback @Trevor
2

You could try this:

var button = document.getElementById("Button");
var form = document.getElementById("new_video");

button.onclick = function(event) {
  event.preventDefault();
  setTimeout(form.submit, 2000);
}

and

<input type="submit" name="commit" value="Upload" class="btn btn btn-primary" id="Button" data-disable-with="Upload">

Hope this helps.

4 Comments

might want to change to a regular function in the setTimeout
Why @Trevor? Does arrow function have some drawback?
You restricted yourself to es6 browsers only. In 2020 it should be safe, but if you are targeting es6, you should use const. not var. You can also do this setTimeout(form.submit, 2000); but that's confusing for beginners
You are too forward @Trevor ! Thank you for useful clarifications!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.