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); 

  });
有帮助吗?

解决方案

Try to use like this.

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

其他提示

Use setTimeout() and clearInterval()

setTimeout(function(){
   clearInterval(loop);
},2000);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top