简体   繁体   中英

delay in ajax success not working

here is my 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 !

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

$this is button .

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

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

$('#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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM