Create an interval to change image in jQuery?

13,224

Solution 1

new and default are reserved words in javascript. You cannot use them.

To create an interval you should use setInterval:

setInterval(function() {
  // do something
  // ...
}, 1000); // <- 1000ms = 1s

[See it in action]

jQuery(document).ready(function() {

    var timer, imgsrc, cnt = 0;

    $('.video-thumb img').bind('mouseover', function() {

      // if there is no timer
      if (!timer) {

        var $t = $(this);

        // get the image src
        imgsrc = $t.attr('src').replace('default.jpg','');

        // start a new timer
        timer = setInterval(function() {

          // periodically change the src
          $t.attr('src', imgsrc + (cnt+1) + ".jpg");

          // and adjust the counter
          cnt = ( cnt + 1 ) % 3; // 0, 1, 2

        }, 1000); // <- 1000ms = 1s
      }
    }).bind('mouseout',function() {

      // stop rotation
      if (timer) {
        clearInterval(timer);
        timer = null;
      }

      // set the default img
      $(this).attr('src', imgsrc + 'default.jpg');
    });
});

Solution 2

You can see my answer working here:

HTML

<div class="video-thumb"> 
    <img src="http://i2.ytimg.com/vi/Q1yo3mco40U/default.jpg" />
</div>
<div id="counter">0</div>​

JavaScript

$(document).ready(function() {

    var images = [];

    images[0] = "http://i2.ytimg.com/vi/Q1yo3mco40U/default.jpg";
    images[1] = "http://i2.ytimg.com/vi/ivmoCcYLrEk/default.jpg";
    images[2] = "http://i3.ytimg.com/vi/f7d8luQ6p2Q/default.jpg";
    images[3] = "http://i1.ytimg.com/vi/XzFmOKNf8sc/default.jpg";
    images[4] = "http://i2.ytimg.com/vi/-2m1e4g2MFM/default.jpg";
    images[5] = "http://i1.ytimg.com/vi/lK2TSYBh7fw/default.jpg";

    var loop;
    var i = 0;

    var counter = $("#counter");

    $('.video-thumb 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]);
            } 
            counter.html(i);
        }, 1000); 

    }).mouseout(function() {
        clearInterval(loop);
        i = 0;
        $(this).attr('src', images[i]);
        counter.html(i);
    });

});
Share:
13,224
Martti Laine
Author by

Martti Laine

https://github.com/codeclown

Updated on June 04, 2022

Comments

  • Martti Laine
    Martti Laine almost 2 years

    I have a working script like this:

    jQuery(document).ready(function(){
    
        $('.video-thumb img').bind('mouseover',function(){
            var new = $(this).attr('src').replace(/default.jpg/,'1.jpg');
            $(this).attr('src',new);
        }).bind('mouseout',function(){
            var default = $(this).attr('src').replace(/[0-9].jpg/,'default.jpg');
            $(this).attr('src',default);
        });
    
    });
    

    Yeah, you guessed right. It's made to change YouTube's thumbnail on interval. However, I have no idea, how to create the interval. It now changes the thumbnail to 1.jpg, which is another thumbnail, but it should next change the image to 2.jpg in 1 second and so on.

    The whole snippet should probably be written from scratch. Advice?

    Hope you understood :-D

    EDIT: I changed the variable-names from finnish words, I don't use them. Just in this example.

    Martti Laine