javascript - Is there a way to pause or wait for a function to finish before onsubmit action in form -
i have simple example, found out onsubmit requires return value right away. have function pauses 5 seconds , wait till completes returns boolean value simulate other javascript processing action. however, page form submission still triggers before function manages return values. should do, using callback in js in case?
<html> <body> <form action="test.html" onsubmit="return testing();"> <button type="submit"> hello world </button> </form> </body> <script> function testing() { var newstate = -1; settimeout(function() { if (newstate == -1) { alert('video has stopped'); output = "newtext"; console.log("after 5 sec: " + output); return false; } }, 5000); //return false; } </script> </html>
one option call testing() in button click itself. can manually call submit() within timeout function. sure return false after calling testing().
for example:
<form id="myform" action="test.html"> <button type="submit" onclick="testing(); return false;"> hello world </button> </form> <script type="javascript"> function testing() { var newstate = -1; settimeout(function() { if (newstate == -1) { alert('video has stopped'); output = "newtext"; console.log("after 5 sec: " + output); document.getelementbyid("myform").submit(); return false; } }, 5000); //return false; } </script>
btw, there appears no need newstate variable.
Comments
Post a Comment