Javascript递归设置超时

发布于 2024-10-30 18:40:42 字数 747 浏览 1 评论 0原文

我刚刚开始研究 javascript,所以希望这会是简单的事情。我想制作自动播放的图像幻灯片。这非常简单,并且有一些关于它的教程,但由于某种原因我无法让它工作。这就是我所拥有的:

var image1 = new Image();
var image2 = new Image(); 
var image3 = new Image();
image1.src = "images/website6.jpg";
image2.src = "images/website7.jpg";
image3.src = "images/sunset.jpg";
var images = new Array(
  "images/website6.jpg",
  "images/website7.jpg",
  "images/sunset.jpg"
);
setTimeout("delay(images,0)",2000);
function delay(arr,num){
  document.slide.src = arr[num % 3];
  var number = num + 1;
  setTimeout("delay(arr,number)",1000);
}

我试图更改的图像有 id 幻灯片。我也有一些证据表明它有效。发生的事情是加载第一个图像。然后加载第二个图像(这意味着原始的 setTimeout 调用必须有效)。然后什么也没有发生。对我来说这表明递归不起作用。

我对其他语言中的递归非常熟悉,所以我认为这一定只是语法问题或其他问题,但我似乎无法弄清楚。感谢您的任何帮助。

I have just started looking at javascript so hopefully this will be something simple. I want to make a slideshow of images that plays automatically. This is very simple, and there are a few tutorials on it but for some reason I haven't been able to get it to work. This is what I have:

var image1 = new Image();
var image2 = new Image(); 
var image3 = new Image();
image1.src = "images/website6.jpg";
image2.src = "images/website7.jpg";
image3.src = "images/sunset.jpg";
var images = new Array(
  "images/website6.jpg",
  "images/website7.jpg",
  "images/sunset.jpg"
);
setTimeout("delay(images,0)",2000);
function delay(arr,num){
  document.slide.src = arr[num % 3];
  var number = num + 1;
  setTimeout("delay(arr,number)",1000);
}

The image I'm trying to change has id slide. And I also have some evidence that it works. What happens is the first image loads. Then the second image loads (which means the original setTimeout call must be working). Then nothing happens. Which to me suggests it's the recursion that isn't working.

I am very familiar with recursion in other languages, so I think it must just be a syntax thing or something, but I can't seem to figure it out. Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

蓝戈者 2024-11-06 18:40:43

问题是,当您将要评估的字符串传递给 setTimeout 调用时,评估将在全局上下文中完成(稍后,当需要触发时)。因此,您最好(出于许多其他原因)传递实际函数:

setTimeout(function() { delay(images, 0); }, 2000);

function delay(arr, num) {
  document.slide.src = arr[num % 3];
  setTimeout(function() { delay(arr, num + 1); }, 1000);
}

在更多现代浏览器中< /a>,您可以使用函数的 .bind() 方法来创建一个预先绑定到要用作 this 的函数:

setTimeout(delay.bind({arr: images, num: 0}), 2000);

function delay() {
  document.slide.src = this.arr[this.num % 3];
  setTimeout(delay.bind({arr: this.arr, num: this.num + 1}), 1000);
}

六中一,一半- 其他十几个,但只是作为一个例子,表明有多种方法可以做事。

The problem is that when you pass in strings to be evaluated to the setTimeout call, the evaluation will be done (later, when it's time to fire) in the global context. Thus, you're way better off (for a lot of other reasons) passing in actual functions:

setTimeout(function() { delay(images, 0); }, 2000);

function delay(arr, num) {
  document.slide.src = arr[num % 3];
  setTimeout(function() { delay(arr, num + 1); }, 1000);
}

In more modern browsers, you can use the .bind() method for functions to create a function that's pre-bound to something to be used as this:

setTimeout(delay.bind({arr: images, num: 0}), 2000);

function delay() {
  document.slide.src = this.arr[this.num % 3];
  setTimeout(delay.bind({arr: this.arr, num: this.num + 1}), 1000);
}

Six of one, half-dozen of the other, but just as an example that shows there are multiple ways to do things.

泛泛之交 2024-11-06 18:40:43

我对第二个 setTimeout 调用非常怀疑。我会通过使用显式函数与字符串表达式来使其更清楚

setTimeout(function() { delay(arr, number); }, 1000);

I would be very suspicious of the second setTimeout call. I would make it clearer by using an explicit function vs. a string expression

setTimeout(function() { delay(arr, number); }, 1000);
蓝颜夕 2024-11-06 18:40:43

更紧凑一点,并将延迟参数传递给setTimeout()

(function delay(arr, num) {
  document.slide.src = arr[num];
  setTimeout(delay, 1000, arr, (num + 1) % 3);
})(images, 0);

A bit more compact and passing delay and arguments to setTimeout():

(function delay(arr, num) {
  document.slide.src = arr[num];
  setTimeout(delay, 1000, arr, (num + 1) % 3);
})(images, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文