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。我怎样才能做到这一点?感恩

$this是按钮。

4

3 回答 3

8

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

更新:如果您仍想使用 jquery 延迟,请按如下方式编写:

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

演示

那是因为'delay()'的默认队列是'fx',它不会自动包含val(),所以你只需要将它添加到其中。

于 2013-06-27T01:14:39.393 回答
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);
        }
    }
}); 
于 2013-06-27T01:17:46.507 回答
2

使用setTimeout( function , time )是最好的解决方案。
但是如果你想为按钮设置动画,你可以使用jQuery .animate()

var $this = $(this);
$this.val("Saved!").animate(
    { opacity: 0.99 }, //transition
    2000, //duration
    function() { //animation complete
        $this.val("Save");
    });
于 2013-06-27T02:08:54.130 回答