2012-02-21 92 views
0

我正在研究一個jQuery插件。基本上,我需要做的是用setInterval在我的插件中調用一個私有函數。看起來有很多不同的方法來創建一個jQuery插件。我用下面的方法,因爲我覺得它清楚,但如果有需要我改變構建我的插件的方式工作的解決方案,我開...在setInterval()中調用jQuery插件的私有函數

我簡化我的插件只保留問題部分:

(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); 

問題是我想每秒調用一次doSomething()。但我需要保留對我的元素的引用(在$ this變量中)。如果我使用setInterval(doSomething),該函數被調用所有權利,但我沒有$ this變量作爲參數。如果我使用setInterval(「doSomething($ this)」),則該函數未定義,因爲它試圖在全局上下文中找到它。

那麼,什麼將是解決辦法在這裏打電話給我私人的功能,並能夠將參數傳遞給它?

感謝

回答

4

經過一些更多的嘗試和錯誤之後,解決方案比我想象的更簡單。使用匿名函數有助於解決問題。這裏是原來的問題了一些小改動代碼:

(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(); 

(和這裏的的jsfiddle,如果你想嘗試一下:http://jsfiddle.net/p4j6z/

1

不做這樣說,這不會工作::)

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

下面的代碼來證明我的意思,我最後的評論:

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(); 
     }; 

最後一次嘗試:(GOOGLE了它,並發現它在計算器;)

 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) 
     { 
     }; 
+0

即使沒有jQuery和插件,你不能把你的函數引用的setInterval的方式。這裏有一個jsFiddle向你展示:http://jsfiddle.net/ZZXm2/沒有()或者字符串「」是兩種可能性。另一個想法是 – Gabriel 2012-02-21 22:37:14

+0

。設置這個數據(),不通過它,並再次選擇間隔方法 – mindandmedia 2012-02-21 22:44:59

+0

然後,我將無法知道在哪個元素尋找我的數據(),這正是我需要通過$這首先...;) – Gabriel 2012-02-21 22:49:23

0

嘗試從上述回答了這個總結:

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

*注意間隔是調用函數someFunction()時的整數值。