Calling a jQuery plugin's private function in setInterval()

I'm working on a jQuery plugin. Basically, what I need it to do is to call a private function in my plugin with setInterval. It looks like there's a lot of different ways to create a jQuery plugin. I'm using the following method because I find it clear but if there's a working solution that requires me to change the way I structure my plugin, I'm opened...

I simplified my plugin to keep only the problematic part:

(function($){
    $.fn.something = function(options)
    {
        var opts = $.extend({}, $.fn.something.defaults, options);
        return this.each(function()
        {
            var $this = $(this);
            $this.click(function(){
                startSomething($this);
            });
        });
        // Private function
        function startSomething($this)
        {
            // $this is defined
            setInterval(doSomething, 1000);
        };
        // Another private function
        function doSomething($this)
        {
            // $this is undefined
        };
    };
    $.fn.something.defaults = {
        my_option: 'value'
    };
})(jQuery);

The problem is that I want to call doSomething() every second. But I need to keep the reference to my element (in the $this variable). If I use setInterval(doSomething), the function is called all right, but I don't have the $this variable as a parameter. If I use setInterval("doSomething($this)") the function is undefined because it's trying to find it in the global context.

So what would be the solution here to call my private function and be able to pass an argument to it?

Thanks

Answers


After some more trial and error, the solution was more simple than I thought. The use of an anonymous function helps solve the problem. Here's the code of the original question with a few changes:

(function($){
    $.fn.something = function(options)
    {
        var opts = $.extend({}, $.fn.something.defaults, options);
        return this.each(function()
        {
            var $this = $(this);
            $this.click(function(){
                startSomething($this);
            });
        });
        // Private function
        function startSomething($this)
        {
            // $this is defined
            setInterval(function(){
                doSomething($this);
            }, 1000);
        };
        // Another private function
        function doSomething($this)
        {
            // $this is undefined
            $this.toggle();
        };
    };
    $.fn.something.defaults = {
        my_option: 'value'
    };
})(jQuery);

$('#test').something();

(and here's the jsFiddle if you want to try it out: http://jsfiddle.net/p4j6z/)


DONT DO IT THIS WAY, THIS WONT WORK: :)

    function startSomething($this)
    {
        // $this is defined
        setInterval(doSomething($this), 1000);
    };
    // Another private function
    function doSomething($this)
    {
        return function()
        {
            //do your thang!
        }
    };

Here the code to demonstrate what i mean in my last comment:

 var opts = $.extend({}, $.fn.something.defaults, options);
 setInterval(doSomething,1000);
        return this.each(function()
        {
            var $this = $(this);
            $this.click(function(){
                $(this).data('started',true);
            });
        });
        // Another private function
        function doSomething()
        {
            $('sel[data-started="true"]').NowMaybeDoYourThing();
        };

last try: (googled it, and found it on stackoverflow ;)

      var self =this;
        return this.each(function()
        {
            var $this = $(this);
            $this.click(function(){
                var it = $(this);
                setInterval(function(){ 
                   self.doSomethingWithIt(it);
                }
            });
        });
        // Another private function
        function doSomethingWithIt(it)
        {
        };

Try this Summarised from answers above:

setInterval(function(){  someFunction();   }, interval);

*note interval is an integer value for when the function someFunction() is called.


Need Your Help

Android: RunOnUiThread vs AsyncTask

android multithreading performance android-asynctask

I believe Google suggests developers to use AsyncTask.

How does DirectWrite (CreateTextFormat) pick the fallback font(s)?

fonts directwrite

The documentation for CreateTextFormat says nothing about font fallback selection, but if the default (NULL = system) collection is chosen, then DirectWrite clearly implements font fallback. For ex...