如何在循环中停止setInterval

时间:2018-08-06 18:57:59

标签: javascript setinterval clearinterval

我试图做均衡器动画。我没有执行开始动画的功能,但是我不能执行停止动画的功能,因为clearInterval无法正常工作。

我的codepen https://codepen.io/naraxiss/pen/qyMamy

var spans = document.querySelectorAll('span');
function getRandom() {
  return Math.random();
}
function scale(el){
  el.style.transform = 'scaleY(' + getRandom() +')';
}

var myInterval = null;

function startMusic (spans){
  var el = spans;
  for(var i = 0; i < el.length; i++){
      (function(i) {
            myInterval = setInterval(function(){
              scale(el[i]);
            }, 100);
      })(i);
    }  
}

function stopMusic (interval,els){
  
  clearInterval(interval);
  //console.log(els)
  
  for(var i = 0; i < els.length; i++){
    els[i].style.stransform = 'scaleY(0.05)'
     
    }
  
}



document.querySelector('.start').addEventListener('click', function(){
  startMusic(spans);
})

document.querySelector('.finish').addEventListener('click', function(){
  stopMusic(myInterval,spans);
})
body{
  margin: 0;
  
}

span{
  display: inline-block;
  width: 50px;
  height: 300px;
  background-color: #000;
  margin-right: 10px;
  transition: 0.1s linear;
  transform: scaleY(0.005);
  transform-origin: bottom;
}

.equalizer{
  position: absolute;
  top: 50%;
  left: 40%;
  transform: translate(-50%,-50%);
}
<div class="equalizer">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
</div>
<button class="start">START</button>
<button class="finish">FINISH</button>

谢谢!

3 个答案:

答案 0 :(得分:1)

您需要将每个间隔的引用保存在这样的数组中

var myInterval = [];
function startMusic (spans){
  var el = spans;
  for(var i = 0; i < el.length; i++){
      (function(i) {
            let interval = setInterval(function(){
              scale(el[i]);
            }, 100);
            myInterval.push(interval);
      })(i);
    }  
}

然后在stopMusic函数中清除所有这些间隔,就像这样。

function stopMusic (interval,els){

  interval.forEach(inter => clearInterval(inter));
  console.log(els)

  for(var i = 0; i < els.length; i++){
    els[i].style.stransform = 'scaleY(0.05)'

   }

}

答案 1 :(得分:0)

由于在for循环中创建了5个间隔,因此需要分别停止它们。

var spans = document.querySelectorAll('span');

function getRandom() {
  return Math.random();
}

function scale(el) {
  el.style.transform = 'scaleY(' + getRandom() + ')';
}

var myInterval = []; // <------------------------ Make it an array

function startMusic(spans) {
  var el = spans;
  for (var i = 0; i < el.length; i++) {
    (function(i) {
      myInterval.push(setInterval(function() { // <------------- push every item 
        scale(el[i]);
      }, 100));
      console.log(myInterval)
    })(i);
  }
}

function stopMusic(els) {
  for (var x = 0; x < myInterval.length; x++) {
    clearInterval(myInterval[x]); // <--------------------- Access every item to clear
  }
  console.log(els)

  for (var i = 0; i < els.length; i++) {
    els[i].style.stransform = 'scaleY(0.05)'

  }

}

答案 2 :(得分:0)

之所以不起作用,是因为您开始了5个间隔,但只停止了1个。将间隔放入数组中,以便可以循环并停止所有间隔。请参阅下面的更改。

var spans = document.querySelectorAll('span');
function getRandom() {
  return Math.random();
}
function scale(el){
  el.style.transform = 'scaleY(' + getRandom() +')';
}

var myIntervals = [];

function startMusic (spans){
  var el = spans;
  for(var i = 0; i < el.length; i++){
      (function(i) {
            myIntervals[i] = setInterval(function(){
              scale(el[i]);
            }, 100);
      })(i);
    }  
}

function stopMusic (intervals,els){
  
  //clearInterval(interval);
  //console.log(els)
  
  for(var i = 0; i < els.length; i++){
    clearInterval(intervals[i]);
    els[i].style.stransform = 'scaleY(0.05)'
     
    }
  
}



document.querySelector('.start').addEventListener('click', function(){
  startMusic(spans);
})

document.querySelector('.finish').addEventListener('click', function(){
  stopMusic(myIntervals,spans);
})
body{
  margin: 0;
  
}

span{
  display: inline-block;
  width: 50px;
  height: 300px;
  background-color: #000;
  margin-right: 10px;
  transition: 0.1s linear;
  transform: scaleY(0.005);
  transform-origin: bottom;
}

.equalizer{
  position: absolute;
  top: 50%;
  left: 40%;
  transform: translate(-50%,-50%);
}
<div class="equalizer">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
</div>
<button class="start">START</button>
<button class="finish">FINISH</button>