Whenever the user presses a button, a message is created and blinks 5 times. However, each time the button is pressed, all previous messages also blink along with the new one. The goal is to make only the new message blink individually 5 times upon each button press.
Sample HTML:
<div id="result"></div>
<button id="but">Click to Blink Message</button>
JavaScript Code:
var counter = 0;
var i = 0;
function blink(selector){
$(selector).fadeOut('slow', function(){
$(this).fadeIn('slow', function(){
if(counter++ <= 5)
blink(this);
});
});
}
$("#but").click(function() {
i++;
counter = 0;
$('#result').prepend("<div class='num'>Number " + i + "</div>");
blink('.num');
});
The issue might be that there is only one counter being used for blinking, causing all messages to blink together. How can a new counter be created every time the button is pressed? And how does the blink function differentiate which counter to use? Various attempts have been made without success. Any suggestions?