最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - JavaScript setTimeout() function not working on the "onsubmit" form attribute - Stack Overflow

programmeradmin2浏览0评论

I am very new to JavaScript so apologies if I am missing something obvious.

I am trying to display a hidden div upon form submission but instead of having it displayed immediately it needs to appear with a slight delay (e.g. 3 seconds).

I have tried using the setTimeout() function on the 'onsubmit' attribute but the hidden div appears immediately and not with a delay.

Here is a minimal example:

<!DOCTYPE html>
<html>

<body>

  <p>When you submit the form, a function is triggered which alerts some text.</p>

  <form action="/action_page.php" onsubmit="setTimeout(myFunction(), 3000)">
    Enter name: <input type="text" name="fname">
    <input type="submit" value="Submit">
  </form>

  <div id="message" style="display: none">Loading...</div>

  <script>
    function myFunction() {
      document.getElementById("message").style.display = "block"
    }
  </script>

</body>

</html>

I am very new to JavaScript so apologies if I am missing something obvious.

I am trying to display a hidden div upon form submission but instead of having it displayed immediately it needs to appear with a slight delay (e.g. 3 seconds).

I have tried using the setTimeout() function on the 'onsubmit' attribute but the hidden div appears immediately and not with a delay.

Here is a minimal example:

<!DOCTYPE html>
<html>

<body>

  <p>When you submit the form, a function is triggered which alerts some text.</p>

  <form action="/action_page.php" onsubmit="setTimeout(myFunction(), 3000)">
    Enter name: <input type="text" name="fname">
    <input type="submit" value="Submit">
  </form>

  <div id="message" style="display: none">Loading...</div>

  <script>
    function myFunction() {
      document.getElementById("message").style.display = "block"
    }
  </script>

</body>

</html>

Also available here: https://www.w3schools./code/tryit.asp?filename=G3Y3E1YISNH1

So to summarise, setTimeout() is running myFunction but not with the expected delay. I'd be really grateful if someone could help me get my code right please! Thanks.

Share Improve this question edited May 11, 2019 at 21:51 Nazim Kerimbekov 4,7819 gold badges36 silver badges58 bronze badges asked May 11, 2019 at 21:09 vendrediSurMervendrediSurMer 4486 silver badges16 bronze badges 3
  • 4 Try "setTimeout(myFunction, 3000)" (without parentheses). Or "setTimeout(() => myFunction(), 3000)" – Walk Commented May 11, 2019 at 21:12
  • You will most likely need to use event.preventDefault() too to stop the form default action of posting. – NewToJS Commented May 11, 2019 at 21:14
  • Ah did you change the accepted answer? – Praveen Kumar Purushothaman Commented May 11, 2019 at 22:45
Add a ment  | 

4 Answers 4

Reset to default 4

use modern JS conventions. On the JS side, not the HTML side, find your form, and use event listening:

let form = ... // there are many easy ways to get this element

form.addEventListener("submit", evt => {
  // don't actually submit this form to its associated URL:
  evt.preventDefault();
  // and schedule the timeout
  setTimeout(myFunction, 3000);
});

You have to send a function to do a function. Either use:

onsubmit="setTimeout('myFunction()', 3000)"

Or use this way:

onsubmit="setTimeout(function () { myFunction(); }, 3000)"

Also, for not making the form submit, you may try doing a return false:

onsubmit="setTimeout(function () { myFunction(); }, 3000); return false;"

And please use unobtrusive method by using addEventListener.

You are executing the function during assignment. Remove the brackets (). You actually need to pass the reference to the function as a parameter. When adding brackets, you are executing the function and then passing the return value of the function as a parameter.

<form onsubmit="setTimeout(myFunction, 3000)">

Furthermore, you should also not use onsubmit and inline assignments altogether. See this answer for more information.

You are calling the myFunction instead of passing the function as callback to setTimeout. change it setTimeout(myFunction(), 3000) ---> setTimeout(myFunction, 3000)

Thanks

发布评论

评论列表(0)

  1. 暂无评论