Javascript 有没有办法让幻灯片自动移动?

Javascript 有没有办法让幻灯片自动移动?,javascript,recursion,lambda,functional-programming,closures,Javascript,Recursion,Lambda,Functional Programming,Closures,这是我们使用的幻灯片: 这是JS文件: 但是,此幻灯片放映没有使其自动浏览每个图像的设置。你仍然需要点击下面的数字才能看到图片。有人知道如何添加到javascript代码中,使其自动遍历每个图像吗 谢谢 快速浏览源代码后,我没有看到用于自动推进幻灯片的内置API。但是,您可以使用另一种幻灯片,如: 此解决方案使用闭包和递归 var SlideChanger = function(seconds_each) { var index = -1; // on the first cycl

这是我们使用的幻灯片:

这是JS文件:

但是,此幻灯片放映没有使其自动浏览每个图像的设置。你仍然需要点击下面的数字才能看到图片。有人知道如何添加到javascript代码中,使其自动遍历每个图像吗


谢谢

快速浏览源代码后,我没有看到用于自动推进幻灯片的内置API。但是,您可以使用另一种幻灯片,如:


此解决方案使用闭包和递归

var SlideChanger = function(seconds_each) {
  var index = -1; 
  // on the first cycle, index will be set to zero below
  var maxindex = ($(".change_link").length) - 1; 
  // how many total slides are there (count the slide buttons)
  var timer = function() { 
  // this is the function returned by SlideChanger
    var logic = function() { 
    // this is an inner function which uses the 
    // enclosed values (index and maxindex) to cycle through the slides
      if (index == maxindex) 
        index = 0; // reset to first slide
      else
        index++; // goto next slide, set index to zero on first cycle
      $('.slideshow').blinds_change(index); // this is what changes the slide
      setTimeout(logic, 1000 * seconds_each); 
      // schedule ourself to run in the future
    }
    logic(); // get the ball rolling
  }
  return timer; // give caller the function
}

SlideChanger(5)(); // get the function at five seconds per slide and run it
我们在这里所做的是将内部函数
timer
暴露在定义它的函数之外(
SlideChanger
)。此函数(
timer
)可以访问
SlideChanger
index
maxindex
)中定义的变量

现在,我们已经在封闭环境中设置了变量和返回调用方的函数,我们可以在
logic
中设置逻辑引擎。当运行
logic
时,它使用
index
maxindex
来确定下一步应该显示哪个幻灯片,显示幻灯片,并安排自己在将来再次运行

调用时,返回函数调用
逻辑
,以实现滚动。然后,
logic
会无限期地重复,每次运行时都会安排自己在将来运行

总之,我们用一个数字参数
x
调用
SlideChanger
。它返回一个函数,调用该函数后,将每隔
x
秒更改幻灯片

写相同概念的另一种方法

(function(seconds_each) {
  var index = -1; 
  // on the first cycle, index will be set to zero below
  var maxindex = ($(".change_link").length) - 1; 
  // how many total slides are there (count the slide buttons)
  return function() { 
  // this is the function returned by SlideChanger
    var logic = function() { 
    // this is an inner function which uses the 
    // enclosed values (index and maxindex) to cycle through the slides
      if (index == maxindex) 
        index = 0; // reset to first slide
      else
        index++; // goto next slide, set index to zero on first cycle
      $('.slideshow').blinds_change(index); // this is what changes the slide
      setTimeout(logic, 1000 * seconds_each); 
      // schedule ourself to run in the future
    }
    logic(); // get the ball rolling
  }
})(5)(); // get the function at five seconds per slide and run it

JavaScript是一种很好的语言,具有许多函数编程结构,如高阶函数(创建函数或接受函数作为参数的函数)和匿名函数。有关更多信息,请参见

您只需在其上包含一个计时器,并调用百叶窗更改事件。这对我来说是可行的。

Econysis的解决方案会起作用,但它不必要地复杂。这里有一种更简单的方法,使用setInterval、modulo,而不是将其包装在一个额外的函数中:

function startSlideshow(ms) {
    var index = -1;
    var count = $(".change_link").length - 1;
    return setInterval(function() {
        index = (index + 1) % count;
        $('.slideshow').blinds_change(index);
    }, ms);
}

谢谢你的发帖。但是你能告诉我应该在JS文件中的什么地方插入这个吗?在jquery的底部尝试一下。blinds-0.9.JS不起作用。我在结尾处的“}”(jQuery);”前面插入了代码块?我做得对吗?在那之后就试试——在文件的最底部。仍然不起作用。将其放置在“}”(jQuery);”之后的最底部:|我也试过这个,把它放在这个javascript()的末尾。还是不行。我做错了什么(