I have this jquery code:
$(document).ready(function() {
function slideSwitch() {
var $active = $('#slideShow IMG.active');
if ( $active.length == 0 ) $active = $('#slideShow IMG:last');
var $next = $active.next().length ? $active.next()
: $('#slideShow IMG:first');
$active.addClass('last-active');
$next.css({opacity: 0.0})
.addClass('active')
.animate({opacity: 1.0}, 1000, function() {
$active.removeClass('active last-active');
});
}
setInterval( "slideSwitch()", 5000 );
});
And it errors with slideSwitch() is not defined?? any ideas?
If you use a string with
setIntervalit will be evaludated in the scope of thewindowobject, and as you declared the function locally in another function, it’s not available globally.Just use the function name instead of a string with code that calls the function:
This will pass a reference to the function instead of a string, so it doesn’t rely on the name of the function when it’s called.