gpt4 book ai didi

javascript - 无法在 Javascript 中进行递归调用

转载 作者:行者123 更新时间:2023-12-02 10:30:38 25 4
gpt4 key购买 nike

当我尝试运行这个程序时,我在 Firefox 中收到一条错误消息:

moveDate is undefined on line 41

(引用行window.setTimeout("moveDate()",100);

有什么想法吗?我认为递归函数能够定义自己,然后调用自己。

function monthScroller(){
document.getElementById("month").style.visibility = "visible";
var x = 0;
var y = 0;
var dest_x = window.innerWidth/2;
var dest_y = window.innerHeight/2;
var interval = 1;

function moveDate() {
if(x<dest_x){ x = x + interval;}
if(y<dest_y){ y = y + interval;}

document.getElementById("month").style.top = y+"px";
document.getElementById("month").style.left = x+"px";

if ((x+interval < dest_x) && (y+interval < dest_y)) {
window.setTimeout("moveDate()",100);
}
else{
name();
}
}
moveDate();
}

最佳答案

是的,他们是。然而,window.setTimeout("moveDate()",100); 将在全局范围内评估该代码字符串 - 没有 moveDate在那里找到的。相反,将函数引用传递给 setTimout() :

 window.setTimeout(moveDate, 100);

关于javascript - 无法在 Javascript 中进行递归调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10809769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com