Javascript ajax成功延迟不起作用
这是我的ajaxJavascript ajax成功延迟不起作用,javascript,jquery,ajax,Javascript,Jquery,Ajax,这是我的ajax var $this = $(this); $.ajax({ url: "process.php", dataType: 'json' , data :{ method:'POST', id :id , img_val : img_val}, type : 'POST', success: function(outp
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') ;
} }
});
实际上,这个标有-->
的代码并没有延迟工作,它直接显示保存
如果我删除delay(3000).val('Save')
它会显示Saved代码>
我想要的是显示保存代码>然后等待3秒钟,然后显示保存
。我怎样才能做到这一点?蒂纳克斯
$this
是按钮。[更新]
使用setTimeout(函数(){/*您的代码*/},3000)代码>
更新:如果仍要使用jquery延迟,请按以下方式编写:
$('#dd').val('firstVal').delay(2000).queue(function(){$(this).val('SecondVal');}).delay(...;
这是因为'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);
}
}
});
但是,如果要设置按钮的动画,可以使用
delay()
用于动画。阅读文档。我发誓delay()。这个问题每天都会出现..queue()太神奇了!谢谢你的贡献!:)
var $this = $(this);
$this.val("Saved!").animate(
{ opacity: 0.99 }, //transition
2000, //duration
function() { //animation complete
$this.val("Save");
});