延迟ajax成功不起作用

时间:2021-05-29 21:29:17

here is my ajax

这是我的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') ;


               }               }
         }); 

actually this code marked with ---> didnt work with delay it displays directly Save

实际上这个标有--->的代码没有用延迟它直接显示保存

if i remove delay(3000).val('Save') it display Saved !

如果我删除延迟(3000).val('保存')它显示已保存!

and what i want is display Saved ! and then wait 3 seconds and then display Save . how can i achieve this ? thnaks

我想要的是显示保存!然后等待3秒钟然后显示保存。我怎么能实现这个目标? thnaks

$this is button .

这是按钮。

3 个解决方案

#1


7  

[updated] use setTimeout(function(){ /* your code */},3000);

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

update: if you still want to use the jquery delay write it like this:

更新:如果你仍然想使用jquery延迟写这样:

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

DEMO

and that's because the default queue of 'delay()' is 'fx' which doesn't include val() in it automatically, so you just have to add it to it.

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

#2


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

#3


2  

Using setTimeout( function, time ) is the best solution.
But if you want to animate the button, you can make it with jQuery .animate()

使用setTimeout(函数,时间)是最佳解决方案。但是如果你想为按钮设置动画,你可以使用jQuery .animate()

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

#1


7  

[updated] use setTimeout(function(){ /* your code */},3000);

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

update: if you still want to use the jquery delay write it like this:

更新:如果你仍然想使用jquery延迟写这样:

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

DEMO

and that's because the default queue of 'delay()' is 'fx' which doesn't include val() in it automatically, so you just have to add it to it.

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

#2


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

#3


2  

Using setTimeout( function, time ) is the best solution.
But if you want to animate the button, you can make it with jQuery .animate()

使用setTimeout(函数,时间)是最佳解决方案。但是如果你想为按钮设置动画,你可以使用jQuery .animate()

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