I've changed your code a bit:
var interval = setInterval(function(){
change("next");
}, 3000);
var change = function(fn) {
$ds.filter(':visible').fadeOut(function(){
var $div = $(this)[fn]('.article-inform');
if ( $div.length == 0 ) {
if (fn == "next") {
$ds.eq(0).fadeIn();
}
else {
$($ds[$ds.length - 1]).fadeIn();
}
} else {
$div.fadeIn();
}
});
};
$(".preview .prev").on("click", function() {
window.clearInterval(interval);
change("prev");
});
$(".preview .next").on("click", function() {
window.clearInterval(interval);
change("next");
});
Fiddle
I put the slide change in a function called change
, which gets the name of the function that will fetch the next slide, which can be prev
or next
:
$(this)[fn]('.article-inform');
Other than that, I made the slide control stop the interval when a user action changes the slide. I think it's the default behavior, if it was not going to be messed up, with the interval changing the slide along with the user action. You can start the interval again after the action, if you prefer.