3

我正在尝试延迟 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);        
                     }
                   }
                 });
            }
4

2 回答 2

4

delay仅影响 jQuery 的“队列效果”,例如slideUpfadeIn,对于其他事情,您应该使用 JavaScript 的 native setTimeout

于 2012-12-02T16:59:30.667 回答
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");​
于 2012-12-02T17:07:27.300 回答