简体   繁体   English

clearInterval不起作用

[英]clearInterval not working

Hi I am calling function on click when autoswitch = true function also calls setInterval and set autoswitch value to false by clicking again calling clearInterval which doesn't work. 嗨,我在autoswitch = true时单击函数时也调用setInterval并通过再次单击clearInterval来将autoswitch值设置为false,这是行不通的。

Need some help 需要一些帮助

$(document).ready(function() {
  var speed = 500;
  var autoswitch = true;
  var autoswitch_speed = 4000;

  //add active class
  $('.slide').first().addClass('active');

  //hide slides
  $('.slide').hide();

  //show first slide
  $('.active').show();

  // Click next to show next slide
  $('#next').click(function() {
    nextSlide();
  });

  //Onclick go to prev slide
  $('#prev').click(function() {
    prevSlide();
  });

  //play auto slide show
  $('#playBtn').click(function() {
    if (autoswitch === true) {
      var setIntr = setInterval(nextSlide, autoswitch_speed);
      autoswitch = false;
    } else {
      clearInterval(setIntr);
      autoswitch = true;
    }
  });

  // next slide function
  function nextSlide() {
    $('.active').removeClass('active').addClass('oldActive');

    if ($('.oldActive').is(':first-child')) {
      $('.slide').last().addClass('active');
    } else {
      $('.oldActive').prev().addClass('active');
    }

    $('.oldActive').removeClass('oldActive');
    $('.slide').fadeOut(speed);
    $('.active').fadeIn(speed);
  }

  // Prev slide function        
  function prevSlide() {
    $('.active').removeClass('active').addClass('oldActive');

    if ($('.oldActive').is(':first-child')) {
      $('.slide').last().addClass('active');
    } else {
      $('.oldActive').prev().addClass('active');
    }

    $('.oldActive').removeClass('oldActive');
    $('.slide').fadeOut(speed);
    $('.active').fadeIn(speed);
  }
});

You need to declare setIntr outside of the click function. 您需要在click函数之外声明setIntr So you could set it at the top of the ready function. 因此,您可以将其设置在ready函数的顶部。

I have gone and fixed your code here: https://snippetbox.xyz/9eb54a2a1f52dc1f5d40/ 我已经在这里修复了您的代码: https : //snippetbox.xyz/9eb54a2a1f52dc1f5d40/

$(document).ready(function() {
  var speed = 500;
  var autoswitch = true;
  var autoswitch_speed = 4000;
  var setIntr;

  //add active class
  $('.slide').first().addClass('active');

  //hide slides
  $('.slide').hide();

  //show first slide
  $('.active').show();

  // Click next to show next slide
  $('#next').click(function() {
    nextSlide();
  });

  //Onclick go to prev slide
  $('#prev').click(function() {
    prevSlide();
  });

  //play auto slide show
  $('#playBtn').click(function() {
    if (autoswitch === true) {
      setIntr = setInterval(nextSlide, autoswitch_speed);
      autoswitch = false;
    } else {
      clearInterval(setIntr);
      autoswitch = true;
    }
  });

  // next slide function
  function nextSlide() {
    $('.active').removeClass('active').addClass('oldActive');

    if ($('.oldActive').is(':first-child')) {
      $('.slide').last().addClass('active');
    } else {
      $('.oldActive').prev().addClass('active');
    }

    $('.oldActive').removeClass('oldActive');
    $('.slide').fadeOut(speed);
    $('.active').fadeIn(speed);
  }

  // Prev slide function        
  function prevSlide() {
    $('.active').removeClass('active').addClass('oldActive');

    if ($('.oldActive').is(':first-child')) {
      $('.slide').last().addClass('active');
    } else {
      $('.oldActive').prev().addClass('active');
    }

    $('.oldActive').removeClass('oldActive');
    $('.slide').fadeOut(speed);
    $('.active').fadeIn(speed);
  }
});

It is because you are defining setIntr as a local variable to that function. 这是因为您将setIntr定义为该函数的局部变量。 Once it goes out of scope and is called again you no longer have a handle on that interval. 一旦超出范围并再次调用,您将不再有该间隔的句柄。 You need to define the var out of scope somewhere. 您需要在某个地方超出范围定义var。 I would make it a global, but I'm sure there might be better ways 我会使其成为一个全球性公司,但我相信可能会有更好的方法

you just have to add variable " setIntr " out side the if condition as like below: 您只需要在if条件之外添加变量“ setIntr ”,如下所示:

$('#playBtn').click(function() {
  var setIntr;
   if (autoswitch === true) {
    setIntr = setInterval(nextSlide, autoswitch_speed);
    autoswitch = false;
    } else {
     clearInterval(setIntr);
     autoswitch = true;
    }
  });

As you have declared "setIntr" in If Condition which is not accessible in else, so if you want to use it in else as well then you have to declare it out side the if or globally. 正如您在If条件中声明了“ setIntr”一样,在else中无法访问它,因此,如果您也想在else中使用它,则必须在if或global之外声明它。

EDITED: you are trying to set the value in one condition and checking in else. 编辑:您试图在一种情况下设置值,然后在其他情况下签入。 in this case you must have to declare the " setIntr " as global variable. 在这种情况下,您必须将“ setIntr ”声明为全局变量。

so the working code should be like the following: 因此,工作代码应如下所示:

declare " setIntr " globally in document.ready 在document.ready中全局声明“ setIntr

then use the below code: 然后使用以下代码:

$('#playBtn').click(function() {
 if (autoswitch === true) {
   setIntr = setInterval(nextSlide, autoswitch_speed);
   autoswitch = false;
 } else {
  clearInterval(setIntr);
  autoswitch = true;
 }
});

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

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