简体   繁体   English

Javascript clearInterval 不重置倒计时值

[英]Javascript clearInterval not reseting countdown value

I'm making a swiper slider.我正在制作一个 swiper slider。 My goal is to have a countdown that reset's its value every slide change.我的目标是进行倒计时,在每次幻灯片更改时重置其值。 If slides changes automatically the countdown reset's well and starts counting down again on next slide.如果幻灯片自动更改,倒计时重置很好,并在下一张幻灯片上再次开始倒计时。 The problem is that if i click on the navigation dots then i have some sort of double countdown.问题是,如果我点击导航点,那么我会有某种双重倒计时。 I think clearInterval starts another countdown but the previous countdowns are not removed from the thread execution.我认为 clearInterval 会开始另一个倒计时,但之前的倒计时不会从线程执行中删除。

var autoplay = 20000;
var swiper = new Swiper('.swiper-container', {
    pagination: '.swiper-pagination',
    paginationClickable: true,
    watchSlidesProgress: true,
    autoplay: 20000,
    onProgress: elipse
});

function elipse() {
    clearInterval(timer);
    var countdownNumberEl = document.getElementById('countdown-number');
    var countdown = 20;
    countdownNumberEl.textContent = countdown;
    var timer = setInterval(frame, 1000);
    function frame(){
        countdown = --countdown <= 0 ? 20 : countdown;
        countdownNumberEl.textContent = countdown;
    }
}

var swiperBullet = document.getElementsByClassName("swiper-pagination-bullet");
for (var i = 0; i < swiperBullet.length; i++) {
    swiperBullet[i].addEventListener('click', function () {
        elipse();
    });
}

Can somebody help me resolve what is going on?有人可以帮我解决发生了什么吗?

Make timer a global variable, so it won't forget its value by the time you try to use it (local variables do not keep their value, so when you call a function again, timer starts as undefined ).timer设为全局变量,这样在您尝试使用它时它就不会忘记它的值(局部变量不会保留其值,因此当您再次调用 function 时, timerundefined启动)。 Perhaps make the clear conditional, so it doesn't try stopping a timer when there is none (the first call):也许明确条件,所以它不会尝试在没有计时器时停止计时器(第一次调用):

var timer=false;
function elipse() {
  if(timer !== false) {
    clearInterval(timer);
  }
  [...]
  timer = setInterval(frame, 1000);

In your case timer is still not declared and it's undefined在您的情况下, timer仍未声明且未定义

 function elipse() { //timer will be undefined or throw error that is not declared //so you are not clearing anything console.log(timer); clearInterval(timer); //stuff //this wil redaclare timer every time you enter the function and it will return different intervalID //so this will grow exponentially var timer = setInterval(function () { elipse() }, 1000); } elipse()

So you must declare timer out of the scope for function elipse it's not necessary to be a global variable just out of the scope for example:因此,您必须在 scope 中为elipse声明timer ,它不必是 scope 之外的全局变量,例如:

 (function () { var timer; function elipse() { //only the first time will be undefined console.log(timer); clearInterval(timer); //just don't redeclared timer becouse it will be for this scope and it will create a new intervalID that you can't access timer = setInterval(function () { elipse() }, 1000); } elipse() })();

PS clearInterval expects intervalID PS clearInterval需要 intervalID

The identifier of the repeated action you want to cancel.您要取消的重复操作的标识符。 This ID was returned by the corresponding call to setInterval() .此 ID 由对setInterval()的相应调用返回。

And setInterval Return value is a numeric, non-zero valuesetInterval返回值一个数字,非零值

So in my opinion undefined won't cause troubles所以在我看来undefined不会造成麻烦

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM