Reputation: 12012
I have the following issue: I have created some alerts using Bootstrap. Initially, these are hidden. When a rating is given (using raty), an Ajax request is send. When this returns successfully, an alert is shown and a timer is started. When the timer runs out, the alert disappears. I am 100% sure that my Ajax requests are being successful.
There are 2 problems with this.
How can I fix these problems?
Here is my code:
HTML:
<div id='rateSuccess' class='alert alert-success alert-dismissable fade in' role='alert' hidden>
<button class='close' type='button' data-dismiss='alert' aria-label='Close'>
<span aria-hidden='true'>×</span>
</button>
<strong>Success!</strong> Your rating is saved!
</div>
<div id='rateError' class='alert alert-danger alert-dismissable fade in' role='alert' hidden>
<button class='close' type='button' data-dismiss='alert' aria-label='Close'>
<span aria-hidden='true'>×</span>
</button>
<strong>Error!</strong> You must be logged in to give a rating!
</div>
JavaScript:
$(document).ready( function() {
$('#rating').raty({
path: '../images',
click: function(score, evnt) {
$.ajax({
type: 'POST',
url: '/publication/rate/' + pubID,
data: {'score': score},
success: function(data, status, jqXHR) {
var success = data.success;
if (success) {
$('#rateSuccess').toggle();
startTimeout('#rateSuccess');
} else {
var type = data.type;
if (type === 'server') {
$('#serverAlert').toggle();
startTimeout('#serverAlert');
} else if (type === 'loggedin') {
$('#rateError').toggle();
startTimeout('#rateError');
}
}
}
});
}
);
function startTimeout(id) {
window.setTimeout(function() {
$(id).fadeTo(500, 0).slideUp(500, function(){
$(id).toggle();
});
}, 5000);
}
});
JSFiddle: http://jsfiddle.net/yqb1y6k1/
Upvotes: 1
Views: 2815
Reputation: 9157
Remove $(id).toggle();
from your timeout (as the alert is already hidden by .slideUp()
), and set opacity of the element to 1
inside AJAX success
/error
callback:
$('#rateSuccess').css('opacity', 1).slideDown();
.fadeTo()
sets opacity of the element, while .slideUp()
is setting the CSS to display:none;
Upvotes: 2