2013-09-28 49 views
1

我想在一段時間內達到html()輸出。我試過這個,但它不起作用。如何在jquery中使用延遲

if (data==1) { 
    $("#checked_answer"+ques_id).delay(800).html("Correct") ; 

} 
else { 
    $("#checked_answer"+ques_id).delay(600).html("Wrong") ; 
} 
+0

你需要明確按時間推遲,還是直到滿足條件? – Rogue

+0

delay()僅適用於動畫,不適用於其他方法。 – adeneo

+3

使用超時或將代碼放入隊列以便使用delay():'$(「#checked_answer」+ ques_id).clearQueue()。delay(800).queue(function(){$(this).html(「 「);});'如果您使用的是jq 1.9>,而不是clearQueue(),請使用.finish(),這將取消延遲,因爲我知道 –

回答

2

您可以使用

var which = data == 1 ? 'Correct' : 'wrong', 
timeVal = which == 1 ? 800 : 600; 
setTimeout(function(){ 
    $("#checked_answer"+ques_id).html(which) ; 
}, timeVal); 
2

使用超時,而不是作爲delay()僅供外匯隊列:

var delay = data === 1 ? 800  : 600, 
    txt = data === 1 ? 'Correct' : 'Wrong'; 

setTimeout(function() { 
    $("#checked_answer"+ques_id).html(txt); 
}, delay); 

如果你只需要使用delay(),你有你的html()東西添加到隊列:

$('#checked_answer').delay(800).queue(function() { 
    $(this).html('Correct').dequeue(); 
}); 
1
var val = 'correct'; 
var delay =800; 
$setTimeout(function() { 
     $('#checkedAnswer').html(val); }, delay); 
1

如果你想做到這一點使用動畫隊列,那麼你就可以明確添加處理這樣的延遲之後更改文本:

$("#checked-answer").delay(800).queue(function(next) { 
    $(this).html("Correct"); 
    next(); 
}); 

Demo