1

This is a function for a slideshow,onmouseover i want it to stop. Instead of stopping the slideshow onmouseover, it speeds up?? How can i correct this to stop onmouseover?

<body onload="nextslide();">

function nextslide() {
    // Hide current slide
    var object = document.getElementById('slide' + current); //e.g. slide1
    object.style.display = 'none';

    // Show next slide, if last, loop back to front
    if (current == last) {
        current = 1;
    } else {
        current++;
    }

    object = document.getElementById('slide' + current);
    object.style.display = 'block';
    var timeout = setTimeout(nextslide, 2500);

    object.onmouseover = function(){
        clearTimeout( timeout );
    }
    object.onmouseout = nextslide;
}
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
user892134
  • 3,078
  • 16
  • 62
  • 128

1 Answers1

3

I tried your code and the only problem I can see is on "onmouseout" there is an immediate transition to next slide. I would change that line like this:

object.onmouseout = function() {
    timeout = setTimeout(nextslide, 2500);
}

I disagree with Jared, "timeout" is defined there because you are using nested functions and inner functions have access to outer functions scope. You should never omit var when defining variables.

strada
  • 942
  • 9
  • 18
  • That's true about the outer function scope (which I overlooked), although the 'never omit var' I don't. (Note this does mean I advocate polluting the global scope.) – Jared Farrish Aug 13 '11 at 23:33
  • Also, if you tried the code, it would be nice to see it in action (http://jsfiddle.net, for example). – Jared Farrish Aug 13 '11 at 23:35