My task is to make a few pictures in a slideshow and if don't click a button for forward and back (id = right and id = left in my code) in 5 seconds it should automaticly move forward , i succeeded but i feel that the code is to much.
var counter = 1;
$('#left').on('click', function peter() {
clearInterval(time);
time = setInterval(function () {
var id = '#' + counter;
$(id).attr('class', 'hidden');
counter++;
if(counter === 4){
counter = 1;
}
id = '#' + counter;
$(id).attr('class', 'visible');
},3000);
var id = '#' + counter;
$(id).attr('class', 'hidden');
counter--;
if (counter === 0) {
counter = 3;
}
id = '#' + counter;
$(id).attr('class', 'visible');
});
$('#right').on('click', function () {
clearInterval(time);
time = setInterval(function () {
var id = '#' + counter;
$(id).attr('class', 'hidden');
counter++;
if(counter === 4){
counter = 1;
}
id = '#' + counter;
$(id).attr('class', 'visible');
},3000);
var id = '#' + counter;
$(id).attr('class', 'hidden');
counter++;
if(counter === 4){
counter = 1;
}
id = '#' + counter;
$(id).attr('class', 'visible');
});
var time = setInterval(function peter() {
var id = '#' + counter;
$(id).attr('class', 'hidden');
counter++;
if(counter === 4){
counter = 1;
}
id = '#' + counter;
$(id).attr('class', 'visible');
},3000);
So the problem as you can see is that i pretty much decleared my setInterval() 3 times, 1st in my code and then i declear it every time in my eventListeners to reset it. My question is this: Is there any way to declear it once and call the eventListeners like time(call listnener for id='right')
and then in the event listners to just reset it like clearInterval(a)
; without having to retype it.