单击时重置 flex 滑块的计时器

Reset Timer for flex slider on click

我正在尝试按照 w3schools 的示例创建一个弹性滑块。

自动滑动和点击新幻灯片效果非常好,但每次点击按钮时我都想不通如何重置计时器。

是的,我几乎没有使用 jquery/javascript 的经验,我已经在几乎所有我能以某种方式理解的地方测试了 resetTimer()、t.reset() 和更多类似的东西。如果有人能在这里帮助我,我会非常高兴。

感谢 Sam0,我已经编辑了代码 - 有些地方仍然有问题:

<script type="text/javascript">

var slideIndex = 0;
carousel();

var repeater;

function carousel() {
var i;
var x = document.getElementsByClassName("mySlides");
for (i = 0; i < x.length; i++) {
  x[i].style.display = "none";
}
slideIndex++;
if (slideIndex > x.length) {slideIndex = 1}
x[slideIndex-1].style.display = "block";
}

function cycle(){
setInterval(function(){
  carousel();
}, 5000);
}

cycle();

function cycle(r){

if (r){   // if r is true then clear and restart the timer
  clearInterval(repeater); // clear the timer
  repeater = setInterval(function(){ // start the timer
     carousel();
  }, 5000);
} else {
  clearInterval(repeater); // clear and stop the timer if r isn't true
}
}

cycle(true);


var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
showDivs(slideIndex += n);
}

function currentDiv(n) {
cycle(true);
showDivs(slideIndex = n);
}

</script>

如果您从 carousel() 函数中删除 setTimeout( 调用并将其替换为 setInterval(),这将允许您在重复计时器上调用轮播功能。

function cycle(){
   setInterval(function(){ 
      carousel(); 
   }, 5000);
}

cycle();

然后我们可以将 setInterval() 调用分配给一个私有变量(我们之前在函数外定义),然后我们可以将其用作重置和重新启动计时器的句柄:

var repeater;

function cycle(r){

   if (r){   // if r is true then clear and restart the timer
      clearInterval(repeater); // clear the timer
      repeater = setInterval(function(){ // start the timer
         carousel(); 
      }, 5000);
   } else {
      clearInterval(repeater); // clear and stop the timer if r isn't true
   }
}

cycle(true);

现在您可以随时调用 cycle(false) 来停止计时器,调用 cycle(true) 应该重置并重新启动计时器,因为变量 repeater 一次只能保存一个值时间。如果您想在每次单击某个按钮时重置计时器,请将 cycle(true) 添加到 currentDiv() 函数内的任意位置,如下所示:

function currentDiv(n) {
    cycle(true);
    showDivs(slideIndex = n);
}

更新:

下面的大片段包含 OP 编辑​​代码的改编版本和用于修补的 w3schools 代码。

  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  var slideIndex = -1;
  var repeater;

  function carousel() {

    for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";
    } // cycle through and hide all the images

    slideIndex++;

    if (slideIndex > slides.length) {
      slideIndex = 1;
    } else if (slideIndex <= 0) {
      slideIndex = slides.length;
    } // what to do if slideIndex is too high or too low

    slides[slideIndex - 1].style.display = "block";
    // show the relevant slide
  }

  function cycle(r) {

    if (r) { // if r is true then clear and restart the timer
      clearInterval(repeater); // clear the timer
      repeater = setInterval(function() { // start the timer
        carousel();
      }, 5000);
    } else {
      clearInterval(repeater); // clear and stop the timer if r isn't true
    }
  }

  window.onload = function() {
    carousel();
  }; // show the start image on load

  carousel();
  cycle(true);


  function plusDivs(n) {
    cycle(true);
    slideIndex += n - 1;
    carousel();
  }

  function currentDiv(n) {
    cycle(true);
    slideIndex = n - 1;
    carousel();
  }
* {
  box-sizing: border-box
}
body {
  font-family: Verdana, sans-serif;
  margin: 0
}
.mySlides {
  display: none
}
/* Slideshow container */

.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}
/* Next & previous buttons */

.prev,
.next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -22px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
}
/* Position the "next button" to the right */

.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */

.prev:hover,
.next:hover {
  background-color: rgba(0, 0, 0, 0.8);
}
/* Caption text */

.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}
/* Number text (1/3 etc) */

.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}
/* The dots/bullets/indicators */

.dot {
  cursor: pointer;
  height: 13px;
  width: 13px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}
.active,
.dot:hover {
  background-color: #717171;
}
/* Fading animation */

.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}
@-webkit-keyframes fade {
  from {
    opacity: .4
  }
  to {
    opacity: 1
  }
}
@keyframes fade {
  from {
    opacity: .4
  }
  to {
    opacity: 1
  }
}
/* On smaller screens, decrease text size */

@media only screen and (max-width: 300px) {
  .prev,
  .next,
  .text {
    font-size: 11px
  }
}
<h2>Automatic Slideshow</h2>
<p>Change image every 5 seconds:</p>

<div class="slideshow-container">

  <div class="mySlides fade">
    <div class="numbertext">1 / 3</div>
    <img src="http://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%">
    <div class="text">Caption Text</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">2 / 3</div>
    <img src="http://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
    <div class="text">Caption Two</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">3 / 3</div>
    <img src="http://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
    <div class="text">Caption Three</div>
  </div>
  <a class="prev" onclick="plusDivs(-1)">❮</a>
  <a class="next" onclick="plusDivs(1)">❯</a>
</div>
<br>

<div style="text-align:center">
  <span class="dot" onclick="currentDiv(1)"></span> 
  <span class="dot" onclick="currentDiv(2)"></span> 
  <span class="dot" onclick="currentDiv(3)"></span> 
</div>