对象的javascript递归永远不会结束

时间:2015-06-17 01:53:22

标签: javascript jquery recursion

我遇到的问题是我的递归函数没有正确循环,底部是我的代码,我遇到了麻烦,之前的东西只是为了表明我已经工作了很多:)还有一个链接到一个工作示例。我之前使用了大量的嵌套for循环:

function assembleTimeline(targetItem, sequence) {
    var TIMELINE = "";
    var greenAniArray = {options:{}};
    for (S = 0; S < sequence.length; S++) {
        greenAniArray = [];
        for (L = 0; L < sequence.length; L++) {
            alength = greenAniArray.length;
            for (var setting in sequence[S][L]) {
                greenAniArray.push({});

                if (setting == "duration") {
                    greenAniArray[alength]["duration"] = sequence[S][L][setting];
                    delete sequence[S][L]["duration"];
                }else{
                    greenAniArray[alength].push({});
                    for (var option in sequence[S][L][setting]) {
                        greenAniArray[alength]["options"][option] = sequence[S][L][setting][option];
                    }
                }
                if (greenAniArray[alength]["options"]) {
                    delete greenAniArray[alength]["options"]["duration"];
                }
                $(".jsonresults").html(prettyPrint(greenAniArray));
            }
        }
        animationSequence = greenAniArray[alength]["options"];
        animationDuration = greenAniArray[alength]["duration"];

        // Make commands look like this:
        // to($box, 1, {x:50,y:0})

        assembledTimeline += '.to("' + targetItem + '", ' + animationDuration + ', ' + JSON.stringify(animationSequence) + ')';
    }

    TIMELINE= "tl" + assembledTimeline; 
};

当有人建议递归函数时,要通过这个,但是&#34;序列&#34;是一个数组,可以有一个项目,或几十个。我把它放在一起接受来自数组/对象的信息,其动画序列就像我做的那样:http://codepen.io/ajhalls/pen/QbvRbQ

http://codepen.io/ajhalls/pen/bdrmGw?editors=101

$(function() {
  var target = "targetDiv"
  var aniMation = "";
  var sequence = [{
    "duration": ".2",
    "transform": "translate3d(0,0,0)"
  }, {
    "duration": ".2",
    "transform": "translate3d(0, -30px, 0)"
  }, {
    "transform": "translate3d(0, -15px, 0)",
    "duration": ".2"
  }, {
    "transform": "translate3d(0,-4px,0)",
    "duration": ".2"
  }];

  function parseAnimation(target, sequence, count = 0) {
    for (var property in sequence) {

      duration = sequence[property]["duration"];

      delete sequence[property]["duration"];
      aniSequence = sequence[property];
      //$(".output").text(JSON.stringify(aniSequence)); 
      aniMation += '.to("' + target + '", ' + duration + ', ' + aniSequence + ')';
      $(".output").text(aniMation);

      parseAnimation(target, sequence[property], count++);
    }
  }

  parseAnimation(target, sequence);
});

1 个答案:

答案 0 :(得分:1)

您的示例中的序列不需要递归。如果你有包含序列的序列,那么你会想要递归。

$(function() {
  var target = "target"
  var aniMation = "tl";
  var sequence = [{
    "duration": ".2",
    "transform": "translate3d(0,0,0)"
  }, {
    "duration": ".2",
    "transform": "translate3d(0, -30px, 0)"
  }, {
    "transform": "translate3d(0, -15px, 0)",
    "duration": ".2"
  }, {
    "transform": "translate3d(0,-4px,0)",
    "duration": ".2"
  }];

  function parseAnimation(target, sequence)
  {
    for (var property in sequence)
    {
      var s = sequence[property];
      var aniSequence = { "transform": s["transform"], "duration": s["duration"] };

      aniMation += '.to("' + target + '", ' + s.duration + ', ' + JSON.stringify(aniSequence) + ')<br>';
      $("#output").html(aniMation);
    }
  }

  parseAnimation(target, sequence);
});