سؤال

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