递归函数未定义错误
嗨,我有一个递归问题。
我按照 wc3 http://www.w3schools.com/jsref/met_win_settimeout.asp 但我的似乎根本不起作用。
function rotateImages(start)
{
var a = new Array("image1.jpg","image2.jpg","image3.jpg", "image4.jpg");
var c = new Array("url1", "url2", "url3", "url4");
var b = document.getElementById('rotating1');
var d = document.getElementById('imageurl');
if(start>=a.length)
start=0;
b.src = a[start];
d.href = c[start];
window.setTimeout("rotateImages(" + (start+1) + ")",3000);
}
rotateImages(0);
Firebug 抛出错误:
rotateImages is not defined
[Break On This Error] window.setTimeout('rotateImages('+(start+1)+')',3000);
但是如果我将超时更改为:
window.setTimeout(rotateImages(start+1),3000);
它会递归,但不知何故延迟不起作用并给我太多的递归(一秒 7000)
Hi i have a problem with recursion.
i followed this example from wc3 http://www.w3schools.com/jsref/met_win_settimeout.asp
But mine seems to not work at all.
function rotateImages(start)
{
var a = new Array("image1.jpg","image2.jpg","image3.jpg", "image4.jpg");
var c = new Array("url1", "url2", "url3", "url4");
var b = document.getElementById('rotating1');
var d = document.getElementById('imageurl');
if(start>=a.length)
start=0;
b.src = a[start];
d.href = c[start];
window.setTimeout("rotateImages(" + (start+1) + ")",3000);
}
rotateImages(0);
Firebug throws the error :
rotateImages is not defined
[Break On This Error] window.setTimeout('rotateImages('+(start+1)+')',3000);
However if i change the timeOut to :
window.setTimeout(rotateImages(start+1),3000);
It recursives but somehow the delay doesn't work and gives me too much recursion(7000 in a sec)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该避免 eval 的原因有很多,它破坏了范围就是其中之一。将字符串传递给 setTimeout 会导致在计时器超时时对其进行评估。
您应该传递一个函数。
这会立即调用
rotateImages
,然后将其返回值传递给setTimeout
。这没有帮助,因为rotateImages
不返回函数。您可能想要:
或者创建一个匿名函数,将闭包包装在 start 周围并传递它:
后一个选项在浏览器中具有更好的支持。
There are many reasons why
eval
should be avoided, that it breaks scope is one of them. Passing a string to setTimeout causes it to beeval
ed when the timer runs out.You should pass a function instead.
This calls
rotateImages
immediately, then passes its return value tosetTimeout
. This doesn't help sincerotateImages
doesn't return a function.You probably want:
Or create an anonymous function that wraps a closure around start and pass that instead:
The latter option has better support among browsers.
请警惕 W3Schools 的代码。
其他答案给出了解决方案。我只是补充一点,每次调用
rotateImages
函数时,您都会重新创建数组并重复 DOM 选择。这是不必要的。您可以像这样更改您的代码:
Be wary of code from W3Schools.
The other answers give a solution. I'll just add that you're recreating the Arrays and repeating the DOM selection every time the
rotateImages
function is called. This is unnecessary.You can change your code like this:
尝试以下语法:
setTimeout()
期望函数引用作为第一个参数。简单地在那里放置一个函数调用就会将函数的返回值作为参数,这就是延迟不起作用的原因。然而,您第一次尝试评估字符串是一个很好的方法,但不建议这样做。Try this syntax:
setTimeout()
expects a function reference as the 1st parameter. Simply putting a function call there would give the return value of te function as the parameter, this is why the delay did not work. However your first try with evaluating a string was a good approach, but it is not recommended.