簡體   English   中英

延遲ajax成功不起作用

[英]delay in ajax success not working

這是我的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是按鈕。

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

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

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

DEMO

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

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);
        }
    }
}); 

使用setTimeout( 函數時間是最佳解決方案。
但是如果你想為按鈕設置動畫,你可以使用jQuery .animate()

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM