如何使表单提交同步?

问题描述:

由于javascript(包括表单提交)是同步的单线程模型,除了ajax调用.是对的吗?但是我正面临着与此有关的一个问题.

As javascript(including form submission) is synchronous and single thread model except ajax calls. Is that right? But i am facing one issue regarding this.

我要在第1行提交表单,然后关闭弹出窗口.发生的事情是在表单提交之前调用了self.close.所以在这里它表现为异步模式.表单提交是异步过程吗?如果是的话,我怎样才能使代码表单提交同步吗?(我不想使用setTimeOut和ajax)

I am submitting the form at line 1 and then closing the pop up. what happens is self.close get called before form submission. So here it is behaving in asynch mode. Is form submission a asynchronous process? If yes how can i make the code after form submission synchronous?(I dont want to use setTimeOut and ajax)

这是我相关的jsp代码

Here is my relevant jsp code

function clickSave()
 {

    document.form.action="customerAction.do";
    document.form.submit();// line 1
    self.close();// line 2

 }

更新:-看来我需要纠正自己,根据

Update:- Looks like i need to correct myself form submission is asynchronous process as per Is form submit synchronous or async?. So question is how can i make self.close synchronous with form submission

由于javascript(包括表单提交)是同步且单一的线程模型(ajax调用除外).是对的吗?但是我正面临一个问题.

As javascript(including form submission) is synchronous and single thread model except ajax calls. Is that right? But i am facing one issue regarding this.

嗯,JS对于每个事件侦听器都是异步的.Ajax允许异步模式,将结果作为事件返回.

Well, JS is asynchronous for every event listener. Ajax allow asynchronous mode, returning the result as an event.

JS大多不同步:一次仅执行一项功能.但是,在最新版本中,有一些方法可以在JS(1)中创建后台并发"线程.

JS is mostly NOT CONCURRENT: only one function is being performed at time. However, in newest versions there are ways to create background "concurrent" threads in JS (1)

JS在单个线程上运行.(后台线程除外)

JS run on a single thread. (except for background threads)

我要在第1行提交表单,然后关闭弹出窗口.什么发生这种情况是在表单提交之前调用了self.close.

I am submitting the form at line 1 and then closing the pop up. what happens is self.close get called before form submission.

那不可能.

表单提交是异步过程吗?

Is form submission an asynchronous process?

提交是异步的,也就是说,JS将继续运行并结束.

The submission is asynchronous, that is, the JS will continue to run and end.

您可以测试该示例(2).

You may test that will this example (2).

如果是,在提交表单后如何同步代码?(我不想使用setTimeOut和ajax)

If yes how can i make the code after form submission synchronous?(I dont want to use setTimeOut and ajax)

提交将重新加载整个页面,因此您的JS将结束,此后,该窗口将使用格式结果从服务器加载新页面.您可以在此新页面中添加新的JS以继续".

Submit will reload the entire page, so your JS will end and after that, the window will load the new page from the server with the form result. You may include in this new page a new JS to "continue".

如果您不想重新加载所有页面,则必须使用AJAX,在这种情况下,您有2个选择:

If you don't want to reload all the page, you must use AJAX, and in this case, you have 2 options:

  • 异步:您可以将事件侦听器设置为接收和使用结果.
  • 同步:您的页面将一直阻塞,直到结果准备就绪为止.

注意:(1):(2):

<html>
    <body>
        <script type="text/javascript">
        function click2()
        {
            document.getElementById('form1').submit();
            for(var i=0; i < 1000000000; i++);
            window.open('http://www.*.com');
        }
        </script>

        <button onclick="click2();"> click </button>
        <br/><br/>
        <form id="form1" action="http://www.duckduckgo.com" method="get">
            <input type="text" value="hello"/>
        </form>
    </body>
</html>