views:

41

answers:

1

Hi everyone,

I have a function called testimonials() that basically cycles through a set of divs, whereas it animates a div in, animates it out and animates the next one in.

Now, I wanted to make it stop on the current DIV once the mouse is on it, otherwise known as hovering it. And I've made it work using a code I got from another post in this site, I was just wondering if someone could explain it to me because I'm a little new to jQuery and I really want to understand why it is working.

The code is the following:

function testimonials() {
    //function here
}

//to stop on hover

        var timerId = null;
    function startRotation() {
        if (timerId) {
            return;
        }
        timerId = setInterval('testimonials()', 5000);
    }
    function stopRotation() {
        if (!timerId) {
            return;
        }
        clearInterval(timerId);
        timerId = null;
    }

$(function() {
startRotation();
$('.testimonials').hover(stopRotation,startRotation);
});

Thanks a lot! Amit

A: 

So how this is working...let's break it down piece by piece. .hover(func1, func2) is a shortcut for .mouseenter(func1) .mouseleave(func2), meaning the first function executes when your mouse enters the element, the second when it leaves.

First function, when the mouse enters:

function stopRotation() {
    if (!timerId) {
        return;
    }
    clearInterval(timerId);
    timerId = null;
}

This is stopping the interval previously set via setInterval() from running anymore, killing the current loop via clearInterval() and clearing the timerId variable, so the startRotation function restarts the loop when it's ready to (on mouseleave).

Second function, when the mouse leaves:

function startRotation() {
    if (timerId) {
        return;
    }
    timerId = setInterval('testimonials()', 5000);
}

This re-starts the loop by starting a timer to run testimonials() every 5 seconds, but only if there isn't a timer already running (by checkin if timerId is set). It does this via setInterval().

One change I would make, never pass a string into setInterval() or setTimeout(), this runs an eval() internally. Instead just call the function reference directly, like this:

timerId = setInterval(testimonials, 5000);
Nick Craver
so replace timerId = setInterval('testimonials()', 5000); with timerId = setInterval(testimonials, 5000); ?
Amit
also, I'm a little curious, what does that statement even mean? so, for example, the startRotation() function. It first checks to see if timerId has a value. If it does, it stops and does nothing? Otherwise, it sets the variable timerId to "setInterval('testimonials()',5000), and that somehow makes the function start running? So by setting a variable with the setInterval(function), it makes it start running? Also, it doesn't say VAR timerId = setInterval..., as such, what IS timerId???
Amit
@Amit - Yes on the replace :) Yes on the stops and does nothing (so you don't have multiple timers happening, triggering `testimonials` more often that you want). For `setInterval(function, xx)` it's saying "run this function *every* xx milliseconds. `timerId` is declared right before the functions, as a global variable: `var timerId = null;`.
Nick Craver
I see. so I guess the one thing I don't understand is the following. I understand the setInt(function, xx) string, and I understand that when I pass it in, it makes the function run for xx milliseconds. What I don't understand is why, when the setInterval string is passed into a variable, it makes the function run. For example, if I set the variable T to equal 5, I do var T = 5; All's well there. That's all it does, T is now equal to 5. My question is, we set timerId to the setInterval. so timerId equal setInt...We never say run timerId, it just means timerId is the setInt...what makes it run?
Amit
@Amit - Each timer has an integer ID associated with it, just a key to find it again later, it's not a timer object, just an int, a hash table inside the browser (usually the case) maps this ID to the actual timer object it's running. Then comes `clearInterval()` where we're using that ID to tell the browser "find this timer object, kill it".
Nick Craver
@Nick - Got it. Thanks so much!
Amit