开发者

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 d开发者_JAVA技巧oesn'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);
    }
});


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜