views:

72

answers:

6
+1  Q: 

method not defined

(function($){
    $.fn.slideshow = function(){

        function init(obj){

            setInterval("startShow()", 3000);           
        }               

        function startShow(){
            alert('h');
        }
        return this.each(function(){                    
            init(this);
        });

    }
})(jQuery);

i am getting error

startShow is not defined
+7  A: 

Change

setInterval("startShow()", 3000);            

To

setInterval(startShow, 3000);            

When you pass a string to setInterval(), the code inside is executed outside of the current scope. It's much more appropriate to just pass the function anyway. Functions can be passed around just like any other variable if you leave the parenthesis off.

If you need to pass a variable, you can use a solution similar to the one Guffa provided:

setInterval(function () { startShow(myVar); }, 3000);

This creates an anonymous function to be passed as the first argument to setInterval(), and within that anonymous function you have access to variables and functions further up the scope chain.

Andy E
Yes, I did just copy and paste one of my answers [from yesterday](http://stackoverflow.com/questions/3725470/html5-jscript-with-jquery-setinterval-problem/3725495#3725495) ;-)
Andy E
then how i can pass an obj/var to startShow?
coure06
@coure: see my edit.
Andy E
@coure06 if you want to pass parameters use @Guffa's answer..
Gaby
+6  A: 

The startShow function is local to the scope, so when the setInterval method evaluates the string in the global scope, it won't find the function.

Use an anonymous function instead of the string:

setInterval(function(){ startShow(); }, 3000);

As the anonymous function use the locally declared function, a closure is created so that the function still has access to it.

Guffa
Why all the extra code? ;) *Edit:* looking at his code, you're probably right for what this will *end up* being, +1 for the foresight catch.
Nick Craver
A: 
 function startShow()
 {
        alert('h');
 }    
(function($){
        $.fn.slideshow = function(){

            function init(obj){

                setInterval("startShow()", 3000);           
            }               


            return this.each(function(){                    
                init(this);
            });

        }
    })(jQuery);
Oyeme
Why are you putting startShow into the global scope? Bad bad!
xil3
bad or not bad..it works.
Oyeme
That defeats the purpose - he's trying to keep it encapsulated in the plugin, not accessible to everything. And the problem is that he has quotes around startShow()... simple fix.
xil3
A: 

Passing a string to setInterval/setTimeout makes it run an eval() in the background. The function scope is lost in this eval and requires a global reference. Avoid the use of strings in setInterval/setTimeout , it's never needed and causes ambuigity.

(function($){
    $.fn.slideshow = function(){

        var init = function(obj){
            setInterval(startShow, 3000);           
        }               

        var startShow = function(){
            alert('h');
        }

        return this.each(function(){                    
            init(this);
        });

    }
})(jQuery);
BGerrissen
Interpreting != executing, order is not the issue here, it all centers around calling it as a string, your answer doesn't really address *why* that is :)
Nick Craver
I stand corrected, adjusted my awnser.
BGerrissen
A: 

Your setting the time-out to look in the base scope, you need to create an anonymous function in the

Try the following:

(function($){
    $.fn.slideshow = function(){

        function init(obj){

            setInterval(function(){
                startShow();
            }, 3000);           
        }

        function startShow(){
           //Deprecated 
        }


        return this.each(function(){                    
            init(this);
        });

    }
})(jQuery);

Or you can create an object to hold your methods in:

(function($){
    $.fn.slideshow = function(){

        var Functions = {
            startShow : function(obj)
            {
                alert('Starting Show')
            }
        }

        function init(obj){

            setInterval(function(){
                 Functions.startShow(obj)
            }, 3000);           
        }

        return this.each(function(){                    
            init(this);
        });

    }
})(jQuery);
RobertPitt
A: 

This isn't directly relevant to your problem, but if you take a look at this page, you'll notice the best practices when creating your plugins methods:

http://docs.jquery.com/Plugins/Authoring

This is just an example:

(function( $ ){

  var methods = {
    init : function( options ) {
      setInterval(startShow(), 3000);
    },
    startShow : function( ) { 
      alert('h');
    }
  };

  $.fn.slideshow = function( method ) {

    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }    

  };

})( jQuery );
xil3