Fading slideshow flicker / queue problem on mouseover (slideSwitch.js)

Fading slideshow flicker / queue problem on mouseover (slideSwitch.js)

I've adapted code from the slideSwitch.js tutorial by Jon Raasch, which is basically a fading slideshow. The script promotes the 'active' slide to a higher z-index and animates the opacity for a fading effect.

It's working fine with a pause added to stop the slideshow temporarily on mouseover.

The issue I'm having is I'm trying to stop the script from queuing up when repeatedly mousing over/off the slideshow. When this happens it flickers and goes berserk.

I've experimented with stop(true,true) in various locations but haven't got it working properly.

Can someone advise me where to insert this in the following code? Or if I am going about it the wrong way? Code is below...

Cheers

Luke

  1. // slideshow function

  2. function slideSwitch() {
  3.     var $active = $('#hp-featured div.active');
  4.     if ( $active.length == 0 ) $active = $('#hp-featured div:last');
  5.     var $next =  $active.next().length ? $active.next()
  6.         : $('#hp-featured div:first');
  7.     $active.addClass('last-active');
  8.     $next.css({opacity: 0.0})
  9.         .addClass('active')
  10.         .animate({opacity: 1.0}, 1000, function() {
  11.             $active.removeClass('active last-active');
  12.         });
  13. };

  14. var playSlideshow =  setInterval( "slideSwitch()", 5000 );

  15. // pauses the slideshow on mouseover, then plays again on mouseout

  16. $(function() {
  17. $('#hp-featured div').hover(function() {
  18.      clearInterval(playSlideshow);
  19. },
  20. function() {
  21.     playSlideshow =  setInterval( "slideSwitch()", 5000 );
  22. });
  23. });