views:

56

answers:

3

I have a jQuery function that is running on setInterval(); What I want to do is stop the interval when I hover over the div being displayed, and once I hover off the div, start the interval again (aka continue cycling through the divs).

any idea on how to do this in the simplest form possible?

Thanks! Amit

+3  A: 
var interval = setInterval(...) // create the interval

clearInterval(interval) // clear/stop interval in some point where you call it...

make sure also that interval is not out of scope when you call clearInterval()

Reigel
Thanks Reigel, I seem to have solved the issue using this type of method. I was wondering if you could read my second question and see if you can possibly explain the code to me so that I can fully understand why it is working. Thanks for your help!
Amit
@Amit - don't forget to accept correct answers.. ;)
Reigel
My apologies, I'm new to this :)
Amit
A: 

use clearInterval function

sushil bharwani
A: 

There's Reigel's way, which works a treat, or of course just set a flag that your function checks, only doing its processing if the flag isn't set. Particularly convenient if there's already some indication of hovering that you can use (e.g., as opposed to an actual dedicated flag), if you want to do this for several intervals, etc.

var hovering = false;

function theFunctionRunningOnInterval() {
    if (!hovering) {
        // ...
    }
}

and to hook that up, basically:

$("selector for your hover elements").hover(
    function() {
        hovering = true;
    },
    function() {
        hovering = false;
    }
);

Note that those don't declare their own hovering, as Amit's comment below does; they use the hovering declared in the enclosing scope.

More thorough example:

I used a counter here instead of a simple flag, but that's me being paranoid.

HTML:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Test Page</title>
<style type='text/css'>
body {
    font-family:        sans-serif;
}
span.hoverEffect {
    display:            inline-block;
    width:              2em;
    height:             1em;
    border:             1px solid black;
    background-color:   #eee;
    margin-left:        2em;
}
</style>
</head>
<body>
<p>Watch the ticker: <span id='ticker'></span>
<br>...as you move the mouse over and off any of these boxes:
<span class='hoverEffect'></span>
<span class='hoverEffect'></span>
<span class='hoverEffect'></span>
<span class='hoverEffect'></span></p>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'&gt;&lt;/script&gt;
<script type='text/javascript' src='ticktock.js'></script>
</body>
</html>

JavaScript (ticktock.js):

// A scoping function to avoid creating global variables
(function() {

    // Counter for the number of hovering elements
    var hovering = 0;

    // Hook up our hover element. I always use named functions, but you could
    // put anonymous ones here instead.
    $(".hoverEffect").hover(startHover, endHover);

    // Start the ticker. Again, a named function, but that's style/debugging, not critical.
    setInterval(ticker, 500);

    /**
     * ticker: Updates the "ticker" element with the tick count and time.
     */
    function ticker() {
        var tickElement;

        // Anything hovering?
        if (hovering > 0)
        {
            // Yes, don't do anything
            return;
        }

        // Tick/tock
        // If you were really doing this every half second, it would be worth
        // caching the reference to this ticker somewhere rather than looking it up every time
        tickElement = $("#ticker");
        tickElement.html(tickElement.html() === "tick" ? "TOCK" : "tick");
    }

    /**
     * startHover: Called when any "hoverEffect" element receives a mouseenter event
     */
    function startHover() {
        // Increment the hovering flag
        ++hovering;
    }

    /**
     * endHover: Called when any "hoverEffect" element receives a mouseleave event
     */
    function endHover() {
        // Decrement the hovering flag, clamping at zero out of paranoia
        if (hovering > 0) {
            --hovering;
        }
    }
})();
T.J. Crowder
If I use this method, will I then set<pre><code>$('element').hover(function(){ var hovering=true; }, function() {var hovering=false;});</pre></code>?
Amit
@Amit: Close, but not quite -- I've added an example.
T.J. Crowder
@T.J.: I love your coding style. It made me actually understand what you were doing, and I'm truly a newbie to jQuery. Thanks a lot, it seems like you really invested in the comments. Much appreciated!
Amit
@Amit: LOL, thanks. Looks like I forgot to fix up one of my `{` above. (For my own projects, I always put `{` on its own line, but so few people do that that I change it when posting answers.)
T.J. Crowder