繁体   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