I am adding multiple alerts dynamically using JS. I want to add a 5 seconds visibility to individual alerts after which the alert should get disappeared. In my following code, timeout setting applies only to the latest alert added, all previous alerts are loosing their timeout settings.
$(document).ready(function () {
i=0;
$('button').click(showSuccess);
function showSuccess() {
i++;
$("#myAlert").append("<div class='alert alert-success alert-dismissable alteranimation' id='myAlert_"+i+"'> <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button> Success! message sent successfully.</div>");
setTimeout(() => {
$('#myAlert_'+i).hide();
}, 3000);
}
}
I even tried pushing timeouts to an array in hope of preventing them from getting overridden, but nothing worked.
let a= [];
a.push(setTimeout(() => {
$('#myAlert_'+i).hide();
}, 3000));