jQuery 1.4.2:
I have an image. When the mouseover event is triggered, a function is executed that runs a loop to load several images. On the contrary, the mouseout event needs to set the image back to a predetermined image and no longer have the loop executing. These are only for the images with class “thumb”:
$("img.thumb").live("mouseover mouseout", function(event) {
//var foo = $(this).attr('id');
var wait;
var i=0;
var image = document.getElementById(foo);
if (event.type == 'mouseover') {
function incrementimage()
{
i++;
image.src = 'http://example.com/images/file_'+i+'.jpg';
if(i==30) {i=0;}
}
wait = setInterval(incrementimage,500);
} else if (event.type == 'mouseout') {
clearInterval (wait);
image.src = 'http://example.com/images/default.jpg';
}
return false;
});
When I mouseout, the image is set to the default.jpg but the browser continues to loop though the images. It will never stop. Can someone hit me with some knowledge? Thanks.
The problem is that the mouseout event doesn’t see the same
waitthat the mouseover event did. You need to store it somewhere else. I would suggest using [data()][1]to store it on the element.Also, this sequence doesn’t make sense:
thisis already the image element.Lastly, that’s not how I’d define the function. Try something like: