Function is not defined, failed to load sources jQuery

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?

Answers


If you use a string with setInterval it will be evaludated in the scope of the window object, 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:

window.setInterval(slideSwitch, 5000);

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.


Need Your Help

How does Entity Framework insert new values?

c# linq entity-framework

I have another question kind of relative to this but I'd like to separate them for clarity reasons.

Best way of binding onchange event on every form field using Rails 3, UJS/jQuery

jquery ajax ruby-on-rails-3 javascript-events onchange

I want to make form submission on every onchange() form field event to get "live search results count" in separate css box. (Like here when you change some form field value you get immediate "searh

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.