[英]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,有两个问题,
keyup
you're attaching jQuery animations
each time on keyup.在keyup
您每次都在 keyup 上附加jQuery animations
。 So clearing
the interval wont help you.所以clearing
间隔不会帮助你。$(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:您的代码存在多个问题:
stop
method of jQuery要停止动画,您应该使用 jQuery 的stop
方法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.