admin管理员组文章数量:1400149
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.
-
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 theform
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
4 Answers
Reset to default 4use 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
版权声明:本文标题:html - JavaScript setTimeout() function not working on the "onsubmit" form attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744250569a2597230.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论