jQuery settimeout和show

时间:2012-11-14 22:06:41

标签: jquery html css jquery-animate settimeout

对于这个菜鸟问题,我很抱歉,我正在尝试逐个显示三个红色气泡,请在此处查看我的代码:http://jsfiddle.net/FLt9Z/

这部分javascript看起来像这样:

function leftappear() {
    $('#redbubble-left').show();
}

function topappear() {
    $('#redbubble-top').show();
}

function rightappear() {
    $('#redbubble-right').show();
}


var animation_script = [
    {
         at_time: 30 * 1000, // 30 seconds
         animation: leftappear()
    },
    {
         at_time: 31 * 1000, // 31 seconds
         animation: topappear()
    },
    {
         at_time: 32 * 1000, // 32 seconds
         animation: rightappear()
    }
];

var current_time = 0;

function animate_next() {
    var next = animation_script.shift(),
        time_between = next.at_time - current_time;
    setTimeout(function () {
        current_time = next.at_time;
        next.animation();
        animate_next();
    }, time_between);
}

非常感谢你们!

2 个答案:

答案 0 :(得分:4)

两个问题:

  1. 您正在调用动画方法,而不是在animation_script
  2. 中引用它们
  3. 你永远不会触发第一个动画。
  4. http://jsfiddle.net/FLt9Z/1/

    以下是两个主要变化。

    var animation_script = [
        {
             at_time: 2* 1000, // 30 seconds
             animation: leftappear // NOTE: drop the parens ()
        },
        {
             at_time: 3* 1000, // 31 seconds
             animation: topappear // NOTE: drop the parens ()
        },
        {
             at_time: 4* 1000, // 32 seconds
             animation: rightappear // NOTE: drop the parens ()
        }
    ];
    

    然后2,将其踢掉,我只是将animate_next();添加到底部。您可以将它放在顶层或其他位置的init脚本中。这仅用于演示目的。

答案 1 :(得分:2)

除了第一个答案之外,我还想指出你的当前代码一直在调用自己,即使它已经通过显示的气泡列表,而是我只需循环一次并设置超时:< / p>

$.each(animation_script, function(i, animation) {
    var next = animation_script.shift(),
        time_between = next.at_time - current_time;
    setTimeout(function() {
        current_time = next.at_time;
        next.animation();
    }, time_between);
});