I'm having following code.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function sleep( lf_ms ) {
return new Promise( resolve => setTimeout( resolve, lf_ms ) );
}
async function check_form() {
alert( 'Test 1' );
await sleep( 1000 );
alert( 'Test 2' );
return false;
}
</script>
</head>
<body>
<form name="myform" method="post" action="test.htm" onsubmit="return check_form();">
<input type="text" name="city"><br>
<br>
<a href="javascript:check_form();">check the method call via link</a><br>
<br>
<button type="submit">check the method call via submit button</button><br>
<br>
</form>
</body>
</html>
I want to sleep the function check_form() for 1 second.
If I click on the link, "Test 1" and "Test 2" will be displayed. If I click the submit button only "Test 1" is displayed. What am I doing wrong here?
My question is different from Submitting a form with submit() using Promise. Because the javascript event handler onsubmit is not used.
promise
here? – Bloemawait
does not "stop Javascript". It pauses the internal execution of the function, but as soon as you hit the firstawait
in the function, the function returns a promise and other execution continues. So, you're just returning a promise from the submit handler and since the DOM code doesn't know what to do with the promise, it is ignored. There is no way to "stop" Javascript in the way you are thinking. – Mathewson