Set the variable when starting the animation. Free the variable when the animation is complete (i.e. now we have a way to determine if the animation is in progress).
Each time you call a function, first check to see if the variable is set, and if there is one, do not continue ( return).
var speed = 'slow';
var imglist = $("#center-photo img");
var inProgress = false;
var listsize = imglist.size();
imglist.filter(':first').show();
$("#total").html(listsize);
$('#prev').click(function() {
if (inProgress) {
return;
} else {
inProgress = true;
}
var active = imglist.filter(':visible');
var prev = active.prev();
if (prev.size() == 0) {
prev = imglist.filter(':last');
}
var pos = active.position();
var curid = $("#outof").html();
if(curid == 1) {
$("#outof").html(listsize);
}else{
$("#outof").html(curid - 1);
}
active.animate(
{
left: (pos.left + 250),
opacity: 'hide'
},
{
duration: speed,
complete: function() {
inProgress = false;
prev.css('opacity', 0).show().css('left', (pos.left - 250) + "px");
prev.animate(
{
left: pos.left,
opacity: 1
}, {
duration: speed
});
}
});
});
$('#next').click(function() {
if (inProgress) {
return;
} else {
inProgress = true;
}
var active = imglist.filter(':visible');
var next = active.next();
if (next.size() == 0) {
next = imglist.filter(':first');
}
var pos = active.position();
var curid = $("#outof").html();
if(curid == 5) {
$("#outof").html("1");
}else{
var newValue = parseInt(curid) + 1;
$("#outof").html(newValue);
}
active.animate(
{
left: (pos.left - 250),
opacity: 'hide'
},
{
duration: speed,
complete: function() {
inProgress = false;
next.css('opacity', 0).show().css('left', (pos.left + 250) + "px");
next.animate(
{
left: pos.left,
opacity: 1
}, {
duration: speed
});
}
});
});
source
share