I'm trying to get a simple image cycle (no transitions) to work where images begin to cycle on mouseover, and stop cycling on mouseout. This works, except for stopping on mouseout. I'm having a hard time figuring out how to clear the interval:
var itemInterval = 750;
var numberOfItems = $('.portrait img').length;
//set current item
var currentItem = 0;
//show first item
$('.pimg').eq(currentItem).show();
$('.portrait').hover(function() {
//loop through the items
var infiniteLoop = setInterval(function(){
$('.pimg').eq(currentItem).hide();
if(currentItem == numberOfItems -1){
currentItem = 0;
}else{
currentItem++;
}
$('.pimg').eq(currentItem).show();
}, itemInterval);
},
function() {
clearInterval(infiniteLoop);
});
How can I get that second-to-last line working?
Edit: fixed a couple of non-code typos