2015-02-24 52 views
0

Ajax意味着异步,但它看起来像停止我的JavaScript执行或至少暂停它并恢复响应。仅限Chrome浏览器:ajax冻结我的JavaScript执行

HTML值

<input value="foo" data-loading-text="bar" class="foo"> 

扩展的jQuery - >

$.fn.bootstrapButton= function(type){ 
    $el = $(this); 
    loadingText = $el.attr("data-loading-text"); 
    currentValue = $el.val(); 
    if(type === "loading") 
     $el.attr("data-loading-text",currentValue) 
     .val(loadingText) 
     .prop("disabled", true) 
    else if(type === "reset") 
     $el.val(loadingText) 
     .prop("disabled", false) 
     .attr("data-loading-text", currentValue) 
} 

函数调用 - >

save= function (name){ 
    $save = $(".ok") 
    $save.bootstrapButton("loading") 
    $.ajax({ 
    type: 'POST' 
    url: '/server' 
    cache: false 
    dataType: 'json' 
    data:"ss" 
    success: function(response){ 
     alert("success") 
    }).always(function(){ 
    $save.bootstrapButton("reset") 
    }) 
} 

我延长jQuery用户界面的按钮问题的引导程序的按钮堂妹。但是 - 当这个执行的时候,我看不到加载文本,直到ajax请求完成! da faq !.异步不是真正的异步?

顺便说一句,代码工作没有任何毛刺(我可以看到加载文本)与这个小的修改。

save= function (name){ 
    $save = $(".ok") 
    $save.bootstrapButton("loading") 
    setTimeout(funtion(){ 
    $.ajax({ 
     type: 'POST' 
     url: '/server' 
     cache: false 
     dataType: 'json' 
     data:"ss" 
     success: function(response){ 
     alert("success") 
     }).always(function(){ 
     $save.bootstrapButton("reset") 
    }) 
    },100) 
} 

即,在100毫秒的延迟下,加载文本出现!,什么给出?

+0

我注意到这发生在Chrome而不是Firefox。你是Chrome开发人员吗? – Andy 2015-02-24 15:06:56

+0

是否有可能将这些ajax调用的'async'选项设置为false? jQuery ajax调用支持一种使呼叫同步的方法。因此,如果将异步设置为false,这可以解释它...尝试将mannualy设置为true(async:true,...)。只是猜测在这里,我不能更有帮助 – astian 2015-02-24 15:07:09

+0

警报与它无关..是的,看到在铬。 havnt检查了火狐 – 2015-02-24 15:07:53

回答

0

添加下面正如我在阿贾克斯的查询选项来解决这个问题。

异步:真

2

建设:

$.ajax({ 
.... 
}).always(function(){ 
    $save.bootstrapButton("reset") 
    }) 

说:“执行此命令的Ajax,当它完成始终运行这个匿名函数

所以,你已经明确表示,”等到Ajax调用做”,它的工作。

+0

加载文本不见@ bobdye。请再读一遍我的问题。 – 2015-02-24 15:14:38

+0

他读得很好。它不会被看到,浏览器不会一次更新DOM,它会一直等到JavaScript函数返回。 – meskobalazs 2015-02-24 15:24:21