递归函数未定义错误

发布于 2024-11-14 16:18:20 字数 931 浏览 2 评论 0原文

嗨,我有一个递归问题。

我按照 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舞袖。长 2024-11-21 16:18:20

应该避免 eval 的原因有很多,它破坏了范围就是其中之一。将字符串传递给 setTimeout 会导致在计时器超时时对其进行评估。

您应该传递一个函数。

window.setTimeout(rotateImages(start+1),3000);

这会立即调用 rotateImages,然后将其返回值传递给 setTimeout。这没有帮助,因为 rotateImages 不返回函数。

您可能想要:

window.setTimeout(rotateImages,3000,[start+1]);

或者创建一个匿名函数,将闭包包装在 start 周围并传递它:

window.setTimeout(function () { rotateImages(start + 1); },3000);

后一个选项在浏览器中具有更好的支持。

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 be evaled when the timer runs out.

You should pass a function instead.

window.setTimeout(rotateImages(start+1),3000);

This calls rotateImages immediately, then passes its return value to setTimeout. This doesn't help since rotateImages doesn't return a function.

You probably want:

window.setTimeout(rotateImages,3000,[start+1]);

Or create an anonymous function that wraps a closure around start and pass that instead:

window.setTimeout(function () { rotateImages(start + 1); },3000);

The latter option has better support among browsers.

千纸鹤 2024-11-21 16:18:20

请警惕 W3Schools 的代码。

其他答案给出了解决方案。我只是补充一点,每次调用 rotateImages 函数时,您都会重新创建数组并重复 DOM 选择。这是不必要的。

您可以像这样更改您的代码:

(function() {
    var a = ["image1.jpg","image2.jpg","image3.jpg", "image4.jpg"];
    var c = ["url1", "url2", "url3", "url4"];
    var b = document.getElementById('rotating1');
    var d = document.getElementById('imageurl');

    function rotateImages(start) {
      b.src = a[start];
      d.href = c[start];
      window.setTimeout(function() {
          rotateImages( ++start % a.length );
      }, 3000);
    }

    rotateImages(0);
})();

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:

(function() {
    var a = ["image1.jpg","image2.jpg","image3.jpg", "image4.jpg"];
    var c = ["url1", "url2", "url3", "url4"];
    var b = document.getElementById('rotating1');
    var d = document.getElementById('imageurl');

    function rotateImages(start) {
      b.src = a[start];
      d.href = c[start];
      window.setTimeout(function() {
          rotateImages( ++start % a.length );
      }, 3000);
    }

    rotateImages(0);
})();
锦上情书 2024-11-21 16:18:20

尝试以下语法:

window.setTimeout(function() {
    rotateImages(start+1);
},3000);

setTimeout() 期望函数引用作为第一个参数。简单地在那里放置一个函数调用就会将函数的返回值作为参数,这就是延迟不起作用的原因。然而,您第一次尝试评估字符串是一个很好的方法,但不建议这样做。

Try this syntax:

window.setTimeout(function() {
    rotateImages(start+1);
},3000);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文