Msencenb
Msencenb

Reputation: 5104

jQuery/javascript and setInterval not working correctly within mouseenter

Ok so basically I'm implementing a simple horizontal scroller. The function gets fired when I move the mouse into the correct div but only once and doesn't keep looping after the interval time. Any help appreciated:

$(document).ready(function() {
    $('#toparrow').mouseenter(function(e) {
        var func = scrollroller(1);
        setInterval(func,1);
    }).mouseleave(function() {

    });

    function scrollroller(velocity) {
        $('#roller').animate({left:'+='+velocity},1);
    }
});

Upvotes: 0

Views: 337

Answers (2)

aroth
aroth

Reputation: 54806

The problem is with this line:

var func = scrollroller(1);

This isn't assigning the scrollroller function to func, it is invoking scrollroller() with a parameter of '1' and storing the result in func. You probably want to do something like:

setInterval("scrollroller(1);",1);  //alternately:  setInterval(scrollroller,1,1);

Note that the alternate syntax may have issues in IE.

Upvotes: 0

user578895
user578895

Reputation:

var func = function(){ scrollroller(1); };

Upvotes: 2

Related Questions