I have a web site which displays a portfolio using this scrollbar http://manos.malihu.gr/jquery-custom-content-scroller/ .
I want to have an automatic scroll when the page is fully loaded. When a user pass over the portfolio it stops the scroll and whe, he leaves it starts moving again.
It currently works well but sometimes it's getting slow or sluggish randomly. Is there something wrong with my js function?
Here's the page having problems : http://www.lecarreau.net/new/spip.php?rubrique5 (I need the change the hosting provider, I know the site is slow).
This is the function:
(function($){ var timerId = 0; $(window).load(function(){ $("#carousel").show().mCustomScrollbar( { mouseWheel:false, mouseWheelPixels:50, horizontalScroll:true, setHeight:370, scrollButtons:{ enable: true, scrollSpeed:50 }, advanced:{ updateOnContentResize:true } }); timerId = setInterval(function(){myTimer()},50); $('#carousel').mouseenter(function () { clearInterval(timerId); }); $('#carousel').mouseleave(function () { timerId = setInterval(function(){myTimer()},50); }); }); })(jQuery); function myTimer() { var width = $(".mCSB_container").width(); var left = $(".mCSB_container").position().left; if (width-left>0) { var scrollTo = -left+4; $("#carousel").mCustomScrollbar("scrollTo", scrollTo); } } Is there something obvious I'm missing?
setIntervcal, because: ejohn.org/blog/how-javascript-timers-work For animations you wantWindow.requestAnimationFrame()and not the timeout functions anyway: creativejs.com/resources/requestanimationframe - Mozilla spec: developer.mozilla.org/en/docs/Web/API/…