How can I change text after time using jQuery?

前端 未结 4 731
长情又很酷
长情又很酷 2020-12-06 20:05

I\'ve never used jQuery before, and I would like to ask how to do something that may seem simple to you people. I\'ve looked around but couldn\'t find an answer.

Oka

4条回答
  •  难免孤独
    2020-12-06 20:29

    Here is one way to do it (jsfiddle demo):

    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();
    

    with a little bit modification, you should be able to customize the interval for each message.

提交回复
热议问题