2013-03-28 4 views
3

d3.js에서 연결 전환을 시도했습니다. 이를 위해 배열에서 전환 집합을 정의하고 (시도) .each("end", function())을 사용하여 재귀 호출을 호출하는 함수를 만들어 이전이 완료되면 전환을 시작하지만 아직 결과가 없습니다. 작업d3.js의 전환 대기열. 하나씩

animations = [ function(){ console.log(1); return circle.transition().duration(2e3).attr("cy", Math.random()*300); } , 
        function(){ console.log(2); return rect.transition().duration(3e3).attr("fill", "#"+((1<<24)*Math.random()|0).toString(16)); }, 
        function(){ console.log(3); return circle.transition().duration(1e3).attr("r", Math.random()*500); }, 
        function(){ console.log(4); return circle.transition().duration(2e3).style("fill", "#"+((1<<24)*Math.random()|0).toString(16)); }, 
        function(){ console.log(5); return circle.transition().duration(1e3).attr("cx", Math.random()*500); }] 

재귀 함수

function animate(index){ 
     if(index < animations.length - 1){ 
      index = index + 1 
      return animations[index]().each("end", animate(index)) 
     } else { 
      return true 
     } 
    } 
jsfiddle가 here이다

, this

리스트는 recusive 함수를 이용한 예이다.

감사합니다.

답변

5

거의 다 왔어! 대신

return animations[index]().each("end", animate(index)) 

당신은 참조

return animations[index]().each("end", function() { animate(index) }) 

필요 updated jsFiddle

+0

그래, 정말 가까이! 배열의 요소가 함수 인 방법 각각의 매개 변수에 명시 적 함수가 필요하다고 생각하지 않았습니다. 다시 고마워! – jbkunst

+0

@ user829971 도와 드리겠습니다! 왜이 대답을 받아들이지 않습니까 (또는 upvote)? It 's mah peanuts :) – meetamit

+1

죄송합니다! 나는 기운이 없다. 충분한 명성을 얻지 못한다. @ jbkunst를 대신하여 +1 – jbkunst