这是一个"为什么这有效"而不是"为什么这不起作用?movePic 函数更新几个图像的坐标,并根据需要更改图像,然后调用 setTimeOut(movePic,delay)。因此,movePic 函数在 mnovePic 函数中称为 INSIDE。
-
为什么这不会在函数内部对函数进行无休止的递归?
-
如果 #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
setTimeout
调度稍后调用的函数。 它不会影响当前调用堆栈中执行的代码,实际上也与代码无关。
当setTimeout
回调运行时,上一个调用已经完成;它不在调用堆栈上。
因此,您不会得到堆栈溢出。