2013-06-27 25 views
3

,這裏是我的ajax延遲阿賈克斯成功不工作

  var $this = $(this); 
$.ajax({ 

     url: "process.php", 
     dataType: 'json' , 
     data :{ 
      method:'POST', 
      id :id , 
      img_val : img_val}, 
     type : 'POST', 
     success: function(output_data){ 
       if (output_data.msg == 'taken'){ 

     --->  $this.val('Saved !').delay(3000).val('Save') ; 


       }    } 
     }); 

其實這個代碼打上--->沒有工作與延遲它會顯示直接Save

如果我刪除delay(3000).val('Save')它顯示Saved !

和我想要的是顯示Saved !,然後等待3秒鐘,然後顯示Save。我怎麼能做到這一點? thnaks

$this是按鈕。

+4

'延遲()'是動畫做出來。閱讀文檔。 – epascarello

+0

我發誓'延遲()'在jQuery中造成了比其他任何功能更多的混淆。這個問題每天都會出現。 – alex

回答

7

[更新] 使用setTimeout(function(){ /* your code */},3000);

更新:如果你仍然想使用jQuery的延遲寫這樣的:

$('#dd').val('firstVal').delay(2000).queue(function(){$(this).val('SecondVal');}).delay(...; 

DEMO

,這就是因爲'delay()'的默認隊列是'fx'它不會自動包含val(),所以你只需要將它添加到它。

+0

工作精彩:)謝謝。 –

+0

**。queue()**太棒了!感謝您的貢獻! :) – jherax

3
var $this = $(this); 
$.ajax({ 
    url: "process.php", 
    dataType: 'json', 
    data: { 
     method:'POST', 
     id :id, 
     img_val : img_val 
    }, 
    type: 'POST', 
    success: function(output_data) { 
     if (output_data.msg == 'taken') { 
      $this.val('Saved!'); 
      setTimeout(function() { $this.val('Save'); }, 3000); 
     } 
    } 
}); 
2

使用的setTimeout(功能時間是最好的解決辦法。
但是,如果你想動畫按鈕,你可以用jQuery .animate()

var $this = $(this); 
$this.val("Saved!").animate(
    { opacity: 0.99 }, //transition 
    2000, //duration 
    function() { //animation complete 
     $this.val("Save"); 
    });