如何将 setInterval() 传递给 Javascript class 方法?

How to pass setInterval() into a Javascript class method?

我正在尝试制作照片幻灯片。我正在尝试使用 setInterval() 将计时器设置为每 5 秒 运行,我正在练习 classes 并且不知道如何将它合并到 class.

const list = document.querySelector('.js-gallery');
const items = document.querySelectorAll('.gallery__item');
Array.from(list);

class Gallery {
  constructor(slideshow) {
    this.slideshow = slideshow;
    this.slideCount = list.length;
    this.items = items[0].getBoundingClientRect().width;
    this.currentSlide = 1;
  }

  transitionSlide() {
    if (this.currentSlide < this.slideCount) {
      list.style.transform = `translateX(-${this.currentSlide * this.items}px)`;
      this.currentSlide += 1;
    } else {
      list.style.transform = `translateX(0)`;
      this.currentSlide = 1;
    }
  }

  // Trying to make a method with setInterval() so that the slide runs every 5 seconds.
  

}

const pics = new Gallery(list);
.title {
  width: 100%;
  text-align: center;
}

.gallery {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  padding: 0;
  transition: all 500ms ease;
}


.gallery-container {
  overflow: hidden;
  position: relative;
  width: 1000px;
  margin: 0 auto;
}

.gallery__item {
  list-style: none;
  height: 500px;
  min-width: 1000px;
  background-position: center center;
  background-size: cover;
}
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>
<body>
  <h1 class="title">Gallery of Real Cool JS3 Images</h1>
  <div class="gallery-container">
    <ul class="gallery js-gallery">
      <li class="gallery__item js-gallery-item" style="background-image: url('https://picsum.photos/1000/650/?image=1062')"></li>
      <li class="gallery__item js-gallery-item" style="background-image: url('https://picsum.photos/1000/650/?image=837')"></li>
      <li class="gallery__item js-gallery-item" style="background-image: url('https://picsum.photos/1000/650/?image=1025')"></li>
      <li class="gallery__item js-gallery-item" style="background-image: url('https://picsum.photos/1000/650/?image=237')"></li>
    </ul>
  </div>
  <script src="script.js"></script>
</body>
</html>

尝试这样的事情:

class MyClass {
    constructor() {
        this.startInterval(); // start interval in your constructor
    }
    
    startInterval() {
        const myInterval = setInterval(this.transitionSlide, 5000);
    }
    
    transitionSlide() {
        // ... logic for slideshow
        console.log("Slideshow transitioned.");
    }
}

const cls = new MyClass();

下面是一个工作片段。我已经做到它每 0.5 秒转换一次,以便您可以更快地看到它。如果您想要 5 秒,请将间隔更改为 5000 而不是 500。

一般来说,setInterval 在 class 中的工作方式与在 class 之外没有任何不同。你只需要注意 this 绑定(即,如果你做了 setInterval(this.transitionSlide, 5000),这将不起作用,因为 this 会失去范围,因此 .bind(this)

主要注意事项:

  1. 您想跟踪 setInterval 的 return 值,这是一个间隔 ID。这将使您停止间隔不断发生。

  2. 我们现在有 2 个函数 - startSlideshow 和 stopSlideshow,当您实例化一个新的 Gallery 时,您应该在实例变量上使用 .startSlideshow() 来启动它。

  3. 当您需要使用 items.length 时,您使用 list.length 作为 slideCount,因为这是跟踪单个幻灯片的原因。

const list = document.querySelector('.js-gallery');
const items = document.querySelectorAll('.gallery__item'); // Use this for slide count, not list
Array.from(list); // Doesn't do anything

class Gallery {
  constructor(slideshow) {
    this.slideshow = slideshow;
    this.slideCount = items.length;
    this.items = items[0].getBoundingClientRect().width;
    this.currentSlide = 1;
    this.slideTransitionInterval = null;
  }

  transitionSlide() {
    console.log('invoked');
    if (this.currentSlide < this.slideCount) {
      list.style.transform = `translateX(-${this.currentSlide * this.items}px)`;
      this.currentSlide += 1;
    } else {
      this.stopSlideshow();
      list.style.transform = `translateX(0)`;
      this.currentSlide = 1;
    }
  }

  // Trying to make a method with setInterval() so that the slide runs every 5 seconds.
  startSlideshow() {
    this.stopSlideshow();
    this.slideTransitionInterval = setInterval(this.transitionSlide.bind(this), 500);
  }
  
  stopSlideshow() {
    if (this.slideTransitionInterval) {
      clearInterval(this.slideTransitionInterval);
    }
  }

}

const pics = new Gallery(list);
pics.startSlideshow();
.title {
  width: 100%;
  text-align: center;
}

.gallery {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  padding: 0;
  transition: all 500ms ease;
}


.gallery-container {
  overflow: hidden;
  position: relative;
  width: 1000px;
  margin: 0 auto;
}

.gallery__item {
  list-style: none;
  height: 500px;
  min-width: 1000px;
  background-position: center center;
  background-size: cover;
}
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>
<body>
  <h1 class="title">Gallery of Real Cool JS3 Images</h1>
  <div class="gallery-container">
    <ul class="gallery js-gallery">
      <li class="gallery__item js-gallery-item" style="background-image: url('https://picsum.photos/1000/650/?image=1062')"></li>
      <li class="gallery__item js-gallery-item" style="background-image: url('https://picsum.photos/1000/650/?image=837')"></li>
      <li class="gallery__item js-gallery-item" style="background-image: url('https://picsum.photos/1000/650/?image=1025')"></li>
      <li class="gallery__item js-gallery-item" style="background-image: url('https://picsum.photos/1000/650/?image=237')"></li>
    </ul>
  </div>
  <script src="script.js" defer></script>
</body>
</html>