setInterval和clearInterval使用相同的按钮

时间:2018-09-12 08:18:44

标签: javascript jquery html

嘿,我只是想知道对于我来说,只有一个按钮的setInterval和clearInterval会消失吗?

var checkExist;

function checkOn() {
  checkOff();
  checkExist = setInterval(function() {
    if ($('#advert').css('display') == 'block')
      $('#statsContinue').trigger('click');
  }, 1000); // Speed for check 
}

function checkOff() {
  if (checkExist !== null) {
    clearInterval(checkExist);
    checkExist = null;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<button onclick="checkOn();" style="">Skip Stats On</button>
<button onclick="checkOff();" style="">Skip Stats Off</button>

2 个答案:

答案 0 :(得分:3)

验证checkExist是否为空,是否采取以下措施:

var checkExist = null;

function toggle_check() {
  clearInterval(checkExist);
  if (checkExist !== null) {
    checkExist = null;
  } else {
    checkExist = setInterval(function() {
      if ($('#advert').css('display') == 'block')
        $('#statsContinue').trigger('click');
        console.log('triggered!');
    }, 1000); // Speed for check 
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<button onclick="toggle_check();" style="">Skip Stats On</button>

答案 1 :(得分:0)

通过添加一个切换方法来检查<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <logger name="org.springframework.web" level="DEBUG"/> </configuration> 是否为空,我做了一个快速的解决方法。

checkExist
const checkExist = null;

const checkOn = () => {
  checkExist = setInterval(function() {
    if ($('#advert').css('display') == 'block')
      $('#statsContinue').trigger('click');
  }, 1000); // Speed for check
};

const checkOff = () => {
  if (checkExist !== null) {
    clearInterval(checkExist);
    checkExist = null;
  }
};

const checkSwitch = () => {
  !checkExist ? checkOn() : checkOff();
};