使用jQuery在设置时间后更改文本颜色,并无限重复

时间:2012-09-24 20:19:36

标签: jquery

我找到了这个答案:

How can I change text after time using jQuery?

这基本上可以满足我的需求。但是,不要让它在最后重定向到另一个页面,我希望它能够无限循环地浏览文本消息。显然,我删除了IF部分以停止重定向,但是如何让它反复循环显示短信呢?

function nextMsg() {
    if (messages.length == 0) {
        // once there is no more message, do whatever you want
        alert("redirecting");
    } else {
        // change content of message, fade in, wait, fade out and
        // continue with next message
        $('#message').html(messages.pop()).fadeIn(500).delay(1000)
                                          .fadeOut(500, nextMsg);
    }
};

// list of messages to display var messages = [
//   "Hello!",
//   "This is a website!",
//   "You are now going to be redirected.",
//   "Are you ready?",
//   "You're now being redirected..."
// ].reverse();

// initially hide the message $('#message').hide();

// start animation nextMsg();

2 个答案:

答案 0 :(得分:5)

只需对基本代码进行少量更改:

function nextMsg(i) {
    if (messages.length == i) {
        i = 0;
    }

    $('#message').html(messages[i])
                 .fadeIn(500)
                 .delay(1000)
                 .fadeOut(500, function() {
                     nextMsg(i + 1);
                 });
};

nextMsg(0);​

DEMO: http://jsfiddle.net/8wqv2/

答案 1 :(得分:0)

this这样的东西会起作用吗?