Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 函数进入live({和ajax({beforeSend:gets all fired after success:_Jquery - Fatal编程技术网

Jquery 函数进入live({和ajax({beforeSend:gets all fired after success:

Jquery 函数进入live({和ajax({beforeSend:gets all fired after success:,jquery,Jquery,我有一个简单的代码,在liveclick上:通过ajax调用外部链接 代码如下: $('.ajaxLink').live({ click : function(e) { $this = $(this); var myLink = $this.attr('href'); var myId = $this.attr('id'); e.preventDefault(); $('html,body').anim

我有一个简单的代码,在liveclick上:通过ajax调用外部链接

代码如下:

$('.ajaxLink').live({
    click :  function(e) {

        $this = $(this);
        var myLink = $this.attr('href');
        var myId = $this.attr('id');

        e.preventDefault();
        $('html,body').animate({  // this is fired only AFTER content load
            scrollTop : 0
        }, 1500);

        $.ajax({ type: 'GET',   
             url: myLink,   
             async: false,
             dataType: 'html',

             beforeSend : function() {
                caricaModal.open();  // this is also fired only after content load
             },                      // is just an animation on a div.

             success : function(page) {
                caricaModal.close(); // this is fired at good timing

            // some more code here where I insert (page) in the DOM

            }
        });

    }

});
如果您想知道加载页面需要多少时间,大约需要1.5秒,是的,事实是当我单击时,在加载并插入DOM之前什么都没有发生


有什么想法吗?

这是因为您的同步ajax调用锁定了浏览器,从而暂停了由.animate.创建的动画线程。当您调用.animate时,浏览器在计算下一个$.ajax语句之前不会等待它完成,因此sajax调用会有效地暂停它,直到服务器做出响应。请尝试放置ajax call进入.animate的回调,或者删除async:false选项。

这是否意味着async:和beforeSend:不能共存?嗯。这就是它看起来的样子,但除非你能证明它,否则你无法确定。不过这似乎是有道理的。