Question

How do I stop it after some time? For example 2 seconds. Thanks

$('img').mouseover(function() {
    var image = this;
        loop = setInterval(function() {
            if (i < images.length - 1) {
                i++;
                $(image).attr('src',images[i]);
            } else {
                i = 0;
                $(image).attr('src',images[i]);
            } 
        }, 40); 

  });
Was it helpful?

Solution

Try to use like this.

window.setTimeout(function(){
   clearInterval(loop);
}, 2000);

OTHER TIPS

Use setTimeout() and clearInterval()

setTimeout(function(){
   clearInterval(loop);
},2000);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top