停止If和Else stetment on Counting

时间:2017-10-31 09:59:42

标签: javascript jquery html loops if-statement

我想创建显示加载的页面,而不是随时间显示随机数,但问题是加载切换到随机数时,随机数会再次更改为加载(循环)

这是我的代码:

<div class="spinner" id="box1">
  <div class="double-bounce1"></div>
  <div class="double-bounce2"></div>
</div>
<div class="suhu" style="display:none" id="box2">
 <span style="color: #E74C3C; font-size: 30px; text-align: center;">
    Suhu Anda : </span><span id="decimalgenerate" style="font-size: 30px;"></span><span style="font-size: 30px;">&#8451;<br/></span><span style="color: #E74C3C;"></span>
</div>

这是JS代码:

setInterval(function thing() {

      var b1 = document.getElementById('box1');
      var b2 = document.getElementById('box2');

      if(b1['style'].display == 'none')break; {
        b1['style'].display = 'block';
        b2['style'].display = 'none';

      } else {
        b1['style'].display = 'none';
        b2['style'].display = 'block';

      }


    }, 7000);

1 个答案:

答案 0 :(得分:0)

对于停止间隔使用clearInterval

var myTime = setInterval(function thing() {

      var b1 = document.getElementById('box1');
      var b2 = document.getElementById('box2');

      if(b1['style'].display == 'none')break; {
        b1['style'].display = 'block';
        b2['style'].display = 'none';

      } else {
        b1['style'].display = 'none';
        b2['style'].display = 'block';
      }
    }, 7000);

以及你想要的地方

clearInterval(myTime)

示例:

var myTime = setInterval(function thing() {

      var b1 = document.getElementById('box1');
      var b2 = document.getElementById('box2');

      if(b1['style'].display == 'none') {
        b1['style'].display = 'block';
        b2['style'].display = 'none';
        clearInterval(myTime);
      } else {
        b1['style'].display = 'none';
        b2['style'].display = 'block';
      }
    }, 7000);