ここでは、「なぜこれが機能しないのか」ではなく、「なぜこれが機能するのか」を示します。movePic 関数は、いくつかの画像の座標を更新し、必要に応じて画像を変更してから、setTimeOut(movePic, delay) を呼び出します。したがって、movePic 関数は mnovePic 関数内で呼び出されます。
関数内の関数を無限に再帰しないのはなぜですか?
#1 に対する答えが、setTimeOut が movePic を呼び出したときに最初に movePic から抜け出すというものである場合、setTimeOut の後に来る実際の画像の移動はどのように実行されるのでしょうか?
完全なページはwww.salemharvest.orgにあります。機能は次のとおりです。
function movePic () {
left1 += movement;
left2 += movement;
if (left1 > 500) { // photo1 disappears over right edge
left1 = -510; // photo1 goes to 500 left of window
left2=-510; // photo2 goes to START of window
j=j+1;
if (j > (p - 1)) j = 0;
document.images.animated1.src = preLoad[j].src; // switches to photo
}
else if (left1==0) { // photo1 fully in window
left2 =-1010; // photo2 goes to 510 ABSOLUTE to left of window
j=j+1;
if (j > (p - 1)) j = 0;
document.images.animated2.src = preLoad[j].src; // switches the photo
}
setTimeout (movePic, delay);
photo1.style.left = left1 + "px";
photo2.style.left = left2 + "px";
} // end of movePic function