【发布时间】:2019-09-04 14:49:20
【问题描述】:
我有以下代码。
<!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>
我想让函数 check_form() 休眠 1 秒。
如果我点击链接,将显示“测试 1”和“测试 2”。如果我单击提交按钮,则仅显示“测试 1”。我在这里做错了什么?
我的问题与Submitting a form with submit() using Promise 不同。因为没有使用 javascript 事件处理程序 onsubmit。
【问题讨论】:
-
这里为什么需要使用
promise? -
嗨,Daniyal,我一直在寻找能够让我停止 javascript 并遇到承诺的功能。
-
@RüdigerSchmidt 鉴于您最近的编辑,您注意到下面我的回答了吗?
标签: javascript promise async-await onsubmit