[英]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(...;
那是因為'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.