tags:

views:

25

answers:

1
(function($)
{
    $.fn.myPlugin = function(options)
    {
        var _this;
        var timer1;

        var foo = function(n)
        {
            if (timer1 != null) return; // in action
            timer1 = setInterval("bar("+n+")", 500);
        };

        var bar = function(n)
        {
            ...
            if ( ... ) clearInterval(timer1);
        };                      

        return this.each(function()
        {
            _this = $(this);
            _this.bind("click", function(){ foo(10); });            
        });
    }
})(jQuery);

This doesn't work because "bar is not defined."

+1  A: 

Instead of a string, you need to pass a function which directly references bar, so instead of this:

setInterval("bar("+n+")", 500);

Do this:

setInterval(function() { bar(n) }, 500);

You can see this working here

Also, you need to accept questions to get any future answers, you do this by clicking the checkmark beside the answer that helped you resolve the issue. It gives you rep, the answerer rep, and helps the next googler find the appropriate answer faster.

Nick Craver
Didnt realize 1st argument can be a function instead of string-only.
Phonethics