简体   繁体   English

为什么函数之间的setTimeouts不起作用?

[英]Why aren't my setTimeouts between functions working?

I have a series of raphael animations that I want to fire in a specific sequence. 我有一系列要以特定顺序触发的拉斐尔动画。

  1. Fade in the curve. 淡入曲线。
  2. Fade in the ball. 淡入球。
  3. Animate the ball along the curve. 沿曲线动画化球。

I have a setTimeout between each function, but the animations just fire simultaneously. 我在每个函数之间都有一个setTimeout,但是动画只是同时触发。

View it on JSFiddle or here: 在JSFiddle或此处查看:

Raphael("bounce", 640, 480, function () {
        var r = this,
            p =                 r.path("M0,77.255c0,0,269.393,37.431,412.96,247.653 c0,0,95.883-149.719,226.632-153.309").attr({stroke: "#666", opacity: .0, "stroke-width": 1}),
            len = p.getTotalLength(),
        e = r.circle(0, 0, 7).attr({stroke: "none", fill: "#000", opacity:0}).onAnimation(function () {
                var t = this.attr("transform");
            });
        r.customAttributes.along = function (v) {
            var point = p.getPointAtLength(v * len);
            return {
                transform: "t" + [point.x, point.y] + "r" + point.alpha
            };
        };
        e.attr({along: 0});

        var rotateAlongThePath = true;
        function fadecurve(ca,ba,aa,ab){
            ca.animate({opacity:1},2000);
            setTimeout(fadeball(ba,aa,ab),6000);
        }
        function fadeball(ba,aa,ab) {
               ba.animate({opacity:1},400);
               setTimeout(run(ba, aa,ab),5000);
        }
        function run(ba,aa,ab) {
               ba.animate({opacity:1},400);
               ba.animate({along: aa}, ab, ">", function () {
                ba.attr({along: aa});
            });
        }
        fadecurve(p,e,.9,500);
});   

Use anonymous functions like: 使用匿名函数,例如:

setTimeout(function(){
  fadeball(ba,aa,ab);
},6000);

With setTimeout(fadeball(ba,aa,ab),6000); 使用setTimeout(fadeball(ba,aa,ab),6000); the function fadeball gets called immediately because of () which is what is the problem. 问题()会立即由于()而立即调用函数fadeball

Make sure to call the run function that way also :) 确保也以这种方式调用run函数:)

the problem lie in your function referencing inside your setTimeout 问题在于您的函数在setTimeout中引用

setTimeout(fadeball(ba,aa,ab),6000);

What you are doing here is executing the function and then assigning the result as the first argument of your setTimeout, if you use an anonymous function it will be fine. 您在这里执行的是执行该函数,然后将结果分配为setTimeout的第一个参数,如果您使用匿名函数,那将很好。

setTimeout(function(){
  fadeball(ba,aa,ab);
},6000);

You are calling your functions immediately and then passing the results of that function to setTimeout . 您正在立即调用函数,然后将该函数的结果传递给setTimeout You need to instead pass a function to setTimeout : 您需要改为将一个函数传递给setTimeout

setTimeout(function(){
    fadeball(ba,aa,ab);
},6000);

I'm sorry if this is off-topic, but the way you are doing this is not scalable. 很抱歉,如果这是题外话,但是您执行此操作的方式不可扩展。 While setting repeated timeouts like this works for a few functions, multi-step animations become increasingly cumbersome as the number of steps increase. 像这样设置重复超时仅适用于一些功能时,随着步数的增加,多步动画变得越来越麻烦。 jQuery can sequence animation, but often jQuery's built-in animation queue does not offer the features needed (like waterfall properties). jQuery可以对动画进行排序,但是jQuery的内置动画队列通常不提供所需的功能(例如瀑布属性)。 Here is a more concise, powerful and scalable alternative with Frame.js : 这是Frame.js的更简洁,强大和可扩展的替代方案:

  var ca = p, ba = e, aa = .9, ab = 500;
  Frame(500, function(next){
      ca.animate({opacity:1}, 2000, next);
  });
  Frame(4000, function(next){
      ba.animate({opacity:1}, 400, next);
  });
  Frame(5000, function(next){
      ba.animate({opacity:1}, 400);
      ba.animate({along: aa}, ab, ">", next);
  });
  Frame(function(next){
      ba.attr({ along: aa });
      next();
  });
  Frame.init();

JQuery's animate function has a animation-complete callback so you can fire animations in succession: jQuery的animate函数具有动画完整的回调,因此您可以连续触发动画:

function SecondAnimation() { $('element').animate(....., ThirdAnimation); }
function ThirdAnimation() { $('element').animate(....., FourthAnimation); }
function FourthAnimation() { $('element').animate(.....); }


$('element').animate(....., SecondAnimation);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM