Reputation: 1452
I am trying to have a simple play and pause a photo slider but setInterval wont work for me can anyone see what I am doing wrong
I want it to stop when I click pause.
$('.control').click(function() {
var data = $(this).attr('data-id');
var interval;
switch (data){
case 'play':
console.log('play');
interval = setInterval(slide, 2000);
break;
case 'pause':
console.log('pause');
clearInterval(interval)
break;
default : console.log('nothing');
}
Upvotes: 0
Views: 4569
Reputation: 95242
Your interval
variable is declared inside the function, and goes away when the function exits. The next time the function is called, it has no value, so clearInterval is being passed undefined
. Declare interval
outside the function and it should work.
Upvotes: 3