我试图在jQuery序列中延迟但是拒绝工作。
var old = $('.likes' + id).text();
$('.likes' + id).text("You've already voted").delay(3000).text(old);
现在,旧的文本立即被输入,而'你已经投票'正在显示。
有什么建议吗?
编辑:
根据要求:
HTML:
<li>
<h2><a href="?page=dub&id=%%DUB_ID%%">%%DUB_TITLE%%</a></h2>
<div class="dub">
<iframe width="560" height="315" src="http://www.youtube.com/embed/%%YOUTUBE_ID%%?hd=1" frameborder="0" allowfullscreen></iframe>
<div id="conten_wrapper">
<div id="conten_meta">
<p> Posted by: <a href="?page=profile&id=%%USER_ID%%">%%USERNAME%%</a><br />
Likes: <span class="likes%%DUB_ID%%">%%LIKES%%</span><br />
Date posted: %%DATE%%
</p>
</div>
<div id="conten_social">
<a href="javascript:void(0);" onclick="javascript:makeRequest(\'like\', %%DUB_ID%%)" class="likebtn">Like</a><a href="javascript:void(0);" onclick="javascript:makeRequest(\'dislike\', %%DUB_ID%%)" class="likebtn2">Dislike</a>
</div>
</div>
</div>
</li>
JS:
function makeRequest(name, id){
$.ajax({
type: "GET",
url: "ajax.php",
data: "name=" + name + "&id=" + id,
success: function(msg){
var obj = jQuery.parseJSON(msg);
if(obj.status == "SUCCES"){
$('.likes' + id).html(parseInt($('.likes' + id).html(), 10)+1);
}else {
var old = $('.likes' + id).text();
$('.likes' + id).text("You've already voted").delay(3000).text(old);
}
}
});
}
答案 0 :(得分:4)
delay
仅影响jQuery的“排队效果”,例如slideUp
和fadeIn
,其他内容则应使用JavaScript的原生setTimeout
。
答案 1 :(得分:1)
使用setTimeout执行此操作的示例:http://jsfiddle.net/ShYVR/
var vote = $('.likes' + id)
var text = vote.text();
setTimeout(function() { vote.text(text); }, 3000);
vote.text("You've already voted");