简体   繁体   English

if else 语句中的 clearInterval 和 setInterval

[英]clearInterval and setInterval within if else statement

type in textarea..on textarea change it updates counter of "char_count" once it reaches at 0 it blinks..then hit backspace till counter reaches at 1输入 textarea..on textarea 更改它更新“char_count”的计数器,一旦达到 0 它就会闪烁..然后按退格键直到计数器达到 1

the issue is it wont stop blinking问题是它不会停止闪烁

<div>
<textarea rows="10"></textarea>
<div class="char_count">5 characters remaining</div>
</div>

here is jsfiddle https://jsfiddle.net/mzx79os2/1/这是 jsfiddle https://jsfiddle.net/mzx79os2/1/

I have looked into the fiddle you've attached.我已经研究了你附上的小提琴。 There are two issues,有两个问题,

  1. On keyup you're attaching jQuery animations each time on keyup.keyup您每次都在 keyup 上附加jQuery animations So clearing the interval wont help you.所以clearing间隔不会帮助你。
  2. jQuery animation has to be stopped, use $(element).stop(true, true) jQuery 动画必须停止,使用$(element).stop(true, true)

I have updated the jsFiddle and also added a check isBlinking to attach animation only once.我更新了 jsFiddle 并添加了一个检查isBlinking以仅附加一次动画。

Check this link检查此链接

https://jsfiddle.net/mzx79os2/8/ https://jsfiddle.net/mzx79os2/8/

var descriptionTextarea = $("textarea");
var char_count = ".char_count";
var textMax = 5;
var isRunning = false;
var clearInt = 0;
$(char_count).html(textMax + ' characters remaining');
descriptionTextarea.on("keyup", function(e) {
  e.stopPropagation();

  var textLength = $(this).val().length;
  var textRemaining = textMax - textLength;
  var selfCharCounter = $(this).parent().find(char_count);

  function blink() {
    $(selfCharCounter).fadeOut(500).fadeIn(500);
  };

  if (textRemaining <= 0 && !isRunning) {
    selfCharCounter.css("color", "red");
    clearInt = setInterval(blink, 500);
    isRunning = true;
  } else if (textRemaining > 0 && isRunning) {
    isRunning = false;
    selfCharCounter.css("color", "");
    $(selfCharCounter).stop(true, true).fadeOut();
    $(selfCharCounter).stop(true, true).fadeIn()
    clearInterval(clearInt);
    clearInt = 0;
  }
  selfCharCounter.html(textRemaining + ' characters remaining');
});

There are multiple problems with your code:您的代码存在多个问题:

  1. In your case clearInt is set inside the keyup handler (so after every keypress you reset clearint to 0 so you don't clear the right one)在您的情况下, clearInt 设置在 keyup 处理程序中(因此在每次按键后您将 clearint 重置为 0,这样您就不会清除正确的一个)
  2. You set multiple intervals on every keypress and you just remove the last one, you should only add an interval if there's not one already running您在每个按键上设置多个间隔,然后删除最后一个,如果没有一个已经在运行,您应该只添加一个间隔
  3. To stop the animation you should use the stop method of jQuery要停止动画,您应该使用 jQuery 的stop方法
  4. You should also paste the code with the problem on SO您还应该将带有问题的代码粘贴到 SO

https://jsfiddle.net/mzx79os2/13/ https://jsfiddle.net/mzx79os2/13/

var $descriptionTextarea = $("textarea");
var $char_count = $(".char_count");
var textMax = 5;
var animation, animationInProcess;

$char_count.html(textMax + ' characters remaining');

function blinkStart(selfCharCounter) { 
   console.log('run blink');
   animationInProcess = true;
   animation = setInterval(function(selfCharCounter){
      if( !animationInProcess){
        blinkStop(selfCharCounter)
        return;
      }
      $(selfCharCounter).fadeOut(500).fadeIn(500);
   }, 500, selfCharCounter);
};

function blinkStop(selfCharCounter){
   console.log('stop blink');
   $(selfCharCounter).stop(true, true).show();
   clearInterval(animation);
};

$descriptionTextarea.on("keyup",function(e){
    e.stopPropagation();
    var textLength = $(this).val().length;
    var textRemaining = textMax - textLength;
    var selfCharCounter = $(this).next();

    if(textRemaining <= 0){
      (!animationInProcess) && blinkStart(selfCharCounter);
      selfCharCounter.css("color","red");
    }else{
      animationInProcess = false;
      selfCharCounter.css("color","");
    }

    selfCharCounter.html(textRemaining + ' characters remaining');
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM