小编Rüd*_*idt的帖子

javascript async await 使用 Promise 提交带有 onsubmit 的表单

我有以下代码。

<!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>
Run Code Online (Sandbox Code Playgroud)

我想让函数 check_form() 休眠 1 秒钟。 …

javascript onsubmit promise async-await

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

async-await ×1

javascript ×1

onsubmit ×1

promise ×1