简体   繁体   English

谁能告诉我为什么这一次有效?

[英]Can anyone tell me why this is working once?

I created a simple rainbow in SCSS & jQuery and it works fine, only once. 我在SCSS和jQuery中创建了一个简单的Rainbow,它只能正常运行一次。 Can someone tell me why it doesn't execute multiple times? 有人可以告诉我为什么它不能多次执行吗?

JS JS

function Raibow() {
  $('#rainbow').show();

  setTimeout(function() {
    $('#rainbow .rainbow').addClass('rainbow-effect');
  }, 100);

  $n = 5; $('#rainbow-timing').text($n);
  setTimeout(function() {
    $('#rainbow-message').show('fade', 400);
  }, 900);

  setTimeout(function() {
    function count() {
      $n--;
      if ($n <= 0) {
        $('#rainbow-message').hide('fade', 400);
        $('#rainbow .rainbow').removeClass('rainbow-effect');
        setTimeout(function() { 
            $('#rainbow-timing').text($n); 
        }, 200);
        setTimeout(function() { 
            $('#rainbow').hide(); 
        }, 1000);
        clearInterval($n);
        return;
      }
      $('#rainbow-timing').text($n);
    }
    setInterval(count, 1000);
  }, 1400);
}

$('button').click(Raibow);

It's a jQuery problem? 这是jQuery问题吗? Or the animation works strange? 还是动画效果很奇怪?

You are not clearing the interval properly. 您没有正确清除间隔。

As per the Docs 根据文档

The returned timeoutID is a numeric, non-zero value which identifies the timer created by the call to setInterval(); 返回的timeoutID是一个非零的数字值,它标识通过调用setInterval()创建的计时器。 this value can be passed to Window.clearInterval() to cancel the timeout. 该值可以传递给Window.clearInterval()来取消超时。

https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval https://developer.mozilla.org/zh-CN/docs/Web/API/WindowTimers/setInterval

so you need to pass the timeoutID refreshIntervalId in the below example to clearinterval() 因此,您需要在以下示例中将timeoutID refreshIntervalId传递给clearinterval()

 var refreshIntervalId;

   function count() {
      $n--;
      if ($n <= 0) {
        $('#rainbow-message').hide('fade', 400);
        $('#rainbow .rainbow').removeClass('rainbow-effect');
        setTimeout(function() { 
            $('#rainbow-timing').text($n); 
        }, 200);
        setTimeout(function() { 
            $('#rainbow').hide(); 
        }, 1000);
        clearInterval(refreshIntervalId);
        return;
      }
      $('#rainbow-timing').text($n);
    }

  setTimeout(function() {
    refreshIntervalId =  setInterval(count, 1000);
  }, 1400);

Fiddle: https://jsfiddle.net/405zqzef/ 小提琴: https : //jsfiddle.net/405zqzef/

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

相关问题 谁能告诉我为什么在不处理样式组件之前? - Can anyone tell me why before not working on styled components? 谁能告诉我为什么我的Scrollspy无法正常工作? - Can anyone tell me why my Scrollspy is not working? 谁能告诉我为什么这个循环无法正常工作? - Can anyone tell me why this loop isn't working as it should? 谁能告诉我为什么我的JavaScript点击无法正常工作? - can anyone tell me why my javascript clicks isnt working? 谁能告诉我为什么不返回值? - Can anyone tell me why the value is not returned? 谁能告诉我为什么这个javascript函数调用不起作用? - Can anyone tell me why this javascript function call isn't working? 尝试使用JQuery突出显示表行但不起作用,有人可以告诉我为什么以及如何解决它吗? - Trying to highlight table row using JQuery but not working, can anyone tell me why and how to fix it? 谁能告诉我为什么这个 script.js 不起作用? - Can anyone tell me why this script.js isn't working? 谁能告诉我为什么我的最后2个如果其他语句不起作用? 全新的JavaScript在这里 - Can anyone tell me why my last 2 if else statements are not working? Brand new to JavaScript here 当我将 animation 效果包装在 function 中时,它停止工作。 谁能告诉我为什么? - When I wrap my animation effect in a function, it stops working. Can anyone tell me why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM