0
                $('form[name=addform]').submit(function() {
                    if(error == true){
                        return false;
                    }else{


                        $('#loading').empty();
                        $('#loading').append("<img src = './images/ajax-loader.gif'/>");
                        $('#loading').show();

                        setTimeout(function(){
                              return true;
                        }, 4000);


                    }
                    error = false;
                });

我需要在执行 return true 之前加载 gif 图像。我不为此使用ajax请帮助

4

2 回答 2

3

I'm not exactly sure what you are trying to accomplish, but that code won't work. setTimeout() does not pause the function it has been called from, nor does the return statement in the callback affect it.

于 2013-07-18T10:30:52.437 回答
0

JavaScript 是异步的,而不是同步的 -setTimeout不会“阻止”下一行代码的执行。(参见相关的同步和异步编程有什么区别(在node.js中)

setTimeout要在延迟后提交表单(出于什么目的尚不清楚),您首先需要停止表单提交(通过取消事件),然后在执行回调时再次提交表单。

$('form').on('submit', function(event) {

  // Prevent the form from submitting
  event.preventDefault();

  // 'this' is referring to the event target
  var form = this;

  // It may not be required to wrap the `form.submit` method but it's usually safer to do so
  setTimeout(function() {
    form.submit()
  }, 4000);

});

这是一个例子http://jsfiddle.net/xppNp/1/

于 2013-07-18T10:45:23.127 回答