I'm trying to make this blinking text stop after 3 seconds (or 3-5 blinks). I've set the blink rate to about the right speed, but cannot figure out how to make it stop.
Here's the code:
$('.blink').each(function() {
var elem = $(this);
setInterval(function() {
if (elem.css('visibility') == 'hidden') {
elem.css('visibility', 'visible');
} else {
elem.css('visibility', 'hidden');
}
}, 200);
});
Any suggestions?
I've compiled a jQuery fiddle here: http://jsfiddle.net/M4Fcd/173/
setInterval goes on indefinitely - or until stopped.
What you need to do is capture the intervalID when you create the interval and then clear the interval after you are done with it
var intervalID = setInterval(function....);
// when done
clearInterval(intervalID);
In your particular case:
$('.blink').each(function() {
var elem = $(this);
// count the blinks
var count = 1;
var intervalId = setInterval(function() {
if (elem.css('visibility') == 'hidden') {
elem.css('visibility', 'visible');
// increment counter when showing to count # of blinks and stop when visible
if (count++ === 3) {
clearInterval(intervalId);
}
} else {
elem.css('visibility', 'hidden');
}
}, 200);
});
Updated fiddle http://jsfiddle.net/M4Fcd/186/
You could also use fadeIn/fadeOut like this
$('.blink').each(function() {
var elem = $(this);
elem.fadeOut(100)
.fadeIn(100)
.fadeOut(100)
.fadeIn(100)
.fadeOut(100)
.fadeIn(100);
});
jsFiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With