【问题标题】:Stopping setInterval and executing the rest of the code停止 setInterval 并执行其余代码
【发布时间】:2017-10-08 03:08:09
【问题描述】:

所以如果满足 $('#purple-title').html() != "PLEASE SCAN YOUR CARD" 的条件,我想退出间隔并执行 if() 中的操作功能。

我的问题已经解决了,下面的代码运行正常!

    $(document).ready(function() {
      $.ajaxSetup({
        cache: false
      });
      var refreshID = setInterval(function() {

        $('#purple-title').load('dbcheck.php');
        if ($('#purple-title').html() != "PLEASE SCAN YOUR CARD") {

          clearInterval(refreshID);

          $("#waiting-for-signal").animate({
            opacity: '0'
          }, 300);
          $("#purple-title").animate({
            opacity: '0'
          }, 300);
          setTimeout(function() {
            $("#waiting-for-signal").css("visibility", "hidden");
            $("#purple-title").html("Welcome!");
            $("#screen").prepend('<img id="checked" src="assets/check.gif"/>');
            $("#screen").prepend('<p id="purple-title" class="purple-title" style="margin:20px 0px 0px 0px;">WELCOME<br /><br /><strong>ALEN ŠIMUNIC</strong><br /><br />UID: 31.42.43.25</p>');
            $("#checked").css("opacity", "1");
          }, 300);
          $("#purple-title").animate({
            opacity: '1'
          }, 300);
          setTimeout(function() {
            $("#purple-title").animate({
              opacity: '0'
            }, 300);
            $("#checked").animate({
              opacity: '0'
            }, 300);
          }, 2200);
          setTimeout(function() {
            location.reload();
          }, 2500);
        }

      }, 500);
    });

【问题讨论】:

  • if ($('#purple-title').html() != "PLEASE SCAN YOUR CARD") { clearInterval(refreshID);
  • 这会退出 if() 中的任何脚本执行之前的间隔
  • 它只是不运行下一个间隔
  • @AlenŠimunic 这不是问题......因为你已经在 运行代码 你不需要照顾那个
  • 对不起,我用错了,我会贴出现在可以工作的代码!谢谢大家!

标签: javascript if-statement timeout settimeout setinterval


【解决方案1】:
clearInterval(refreshID);

就是这样……

【讨论】:

  • 对不起,我用错了,我会贴出现在可以工作的代码!谢谢大家!