캔버스에 일부 원을 이동시키는 재귀 함수가 있습니다. Overed 원이 확대되고 (확대) 다른 모든 원이 밀려납니다. 푸시 된 원은 확대가 완료 될 때까지 다른 원을 밀어냅니다.자바 스크립트 재귀 : 최대 호출 스택 크기가 초과되었습니다.
"최대 호출 스택 크기를 초과했습니다"라는 오류 메시지가 나타나지만 문제를 이해하지만 해결 방법을 모르겠습니다 ... 일반적으로 재귀 문제를 해결할 수있는 세 가지 해결책이 있습니다 :
- 는 변경 재귀
- 사용 memoization
- 사용의 setTimeout
을 반복하는하지만 내가 그들 중 누구도 사용하지 수 있다고 생각 :
내가 필요하기 때문에 작업의 알 수없는 횟수의 반복을 구현할 수 없습니다-
내가 충분히 메모이 제이션을 이해하지
- ,하지만 난 그것을 어느 맞지 않는 생각 (또는 어쩌면 내가 틀렸다 누군가가 나에게 말했다 수 다르게?)
- 이 특정 애니메이션에서 함수 호출을 차단해야하므로 SetTimeout을 사용할 수 없습니다.
이 문제를 어떻게 해결합니까? 내가 필요하기 때문에 작업의 알 수없는 횟수의 반복을 구현할 수 없습니다
// Pushes circles aside when some other circle leans on these circles (on zoom in)
var moveCirclesAside = function(circle1, circleToSkip, groupOfMoves) {
var count = circles.length;
for (var i = 0; i < count; i++) {
// Skip the same circle
if (i == circle1.i) {
continue;
}
// Also skip the circle which was intended not to move any further
if (circleToSkip != null && i == circleToSkip.i) {
continue;
}
// Get second circle
var circle2 = circles[i];
// Calculate a distance between two circles
var dx = circle2.x - circle1.x;
var dy = circle2.y - circle1.y;
var distance = Math.sqrt((dx * dx) + (dy * dy));
// If circles already collided need to do some moving...
if (distance <= circle1.r + circle2.r + OD.config.circleSpacing) {
// Get collision angles
var angle = Math.atan2(dy, dx);
var sine = Math.sin(angle);
var cosine = Math.cos(angle);
// Some circle position calculation
var x = OD.config.circleSpacing;
var xb = x + (circle1.r + circle2.r);
var yb = dy * cosine - dx * sine;
// Save each state (move) of any circle to the stack for later rollback of the movement
groupOfMoves.push(copyCircleByVal(circle2));
// Move the circle
circle2.x = circle1.x + (xb * cosine - yb * sine);
circle2.y = circle1.y + (yb * cosine + xb * sine);
// Make sure that circle won't go anywhere out of the canvas
adjustCircleByBoundary(circle2);
// If moved circle leans against some other circles make sure that they are moved accordingly
// And such related moves must be grouped for correct rolback of moves later - so we pass 'groupOfMoves' var
moveCirclesAside(circle2, circle1, groupOfMoves);
}
}
};
답변도 도움이되었지만 슬프게도 단 하나의 답변 만 수락 할 수 있습니다. 감사합니다. – fizis
나는이 감정을 전달하기 위해 당신을 위해 그것을 upvoted했습니다. – agm1984