onmouseover 和 onmouseout setInterval

onmouseover and onmouseout setInterval

我一直被这个问题困扰。我正在为我的应用程序使用滑块。为了滑动我正在使用的应用程序 splide-js。我的图像滑块工作正常。

我已将滑块与 li 元素连接起来。我想制作自定义 onmouseoveronmouseout 事件。当用户在 li 上触发 mouseover 时,它将暂停,当 mouseout 时,它将恢复幻灯片。

我正在检查这个 Stack Overflow post 并尝试通过使用 setinterval 来像这个人一样做逻辑,但是做不到。

const autoPlay = false;
const pauseOnHover = document.getElementById('pause');

function nextSlide() { //I don't what kind logic I should use to pause the slide

}

let  timer = setInterval(function() {
  nextSlide();
}, 400);


pauseOnHover.onmouseover = () => {
  if( autoPlay) {
    timer
  }
}
pauseOnHover.onmouseout = () => {
  if (autoPlay) {
    clearInterval(timer)
  }
}
<div class='checklist' id="pause">
  <ol>
    <li id="link-1">Assign jobs in an instant.</li>
    <li id="link-2">Always see what's happening.</li>
    <li id="link-3">Easy to set-up & use.</li>
    <li id="link-4">Be more sustainable</li>
  </ol>
</div>

请在鼠标悬停时设置和重新设置间隔,如果间隔存在则在鼠标移出时取消它。

请尝试以下代码。

const autoPlay = false;
const pauseOnHover = document.getElementById('pause');

function nextSlide() {
  //For example I have put console.log, you mat put any code block here.
  console.log("hello");
}

let timer;


pauseOnHover.onmouseover = () => {

  autoplay = true;
  if (!autoPlay) {
    autoplay = true;
    timer = setInterval(function() {
      nextSlide();
    }, 400);
  }
}
pauseOnHover.onmouseout = () => {

  autoplay = false;
  timer && clearInterval(timer)

}
<div class='checklist' id="pause">
  <ol>
    <li id="link-1">Assign jobs in an instant.</li>
    <li id="link-2">Always see what's happening.</li>
    <li id="link-3">Easy to set-up & use.</li>
    <li id="link-4">Be more sustainable</li>
  </ol>
</div>