2012-07-24 47 views
0

我試圖使用有名稱的間距按照有方向在這裏建立一個jQuery插件: http://docs.jquery.com/Plugins/Authoring#NamespacingjQuery的名稱空間和setTimeout的

現在,我遇到了一個問題,我的插件需要使用的setTimeout火一個它的方法,

var jScrollerMethods = { 
    ready:function(){ 
     return this.each(function(){ 
     var self = this, 
    $this = $(this), 
     data = $this.data('jScroller'); 

     settings.timeoutHandle = setTimeout(function(){ 
      if(settings.direction = "left"){ 
       this.moveLeft(); 
      }else if(settings.direction = "right"){ 
       this.moveRight(); 
      } 
     }, settings.time); 

     $this.data('jScroller', { 
      settings: settings, 
      element: this 
     }); 
    }); 
} 
$.fn.jScroller = function(call){ 
    if (jScrollerMethods[call]) { 
     return jScrollerMethods[call].apply(this, Array.prototype.slice.call(arguments, 1)); 
    } else if (typeof call === 'object' || ! call) { 
     if (call) { $.extend(settings, call); } 
     return jScrollerMethods.init.apply(this, call); 
    } else { 
     $.error('Method ' + call + ' does not exist on jQuery.jScroller'); 
    } 
} 

但我以爲會發生的setTimeout從窗口不是插件對象解僱,是我想要的插件是可用比上每頁一次,所以我不能只保存當前對象到窗口我怎麼能做到這一點?

+0

我認爲你有問題' this.moveLeft();'?然後使用'self'(或'$ this')。 – 2012-07-24 12:22:18

+0

謝謝,但我現在想出來 – HotHeadMartin 2012-07-24 12:25:22

+0

那麼請提供您的解決方案作爲答案,如果你認爲這將是其他人使用,或刪除你的問題。 – 2012-07-24 12:26:12

回答

0

我發現使用thisreturn this.each的時候會給你確切的選擇結果

所以有一些工作,我已經成功地做到這一點,

var jScrollerMethods = { 
    ready:function(){ 
     selector = this; 

     return this.each(function(){ 
     var self = this, 
    $this = $(this), 
     data = $this.data('jScroller'); 

     settings.timeoutHandle = setTimeout(function(){ 
      if(settings.direction = "left"){ 
       selector.jScroller("moveLeft"); 
      }else if(settings.direction = "right"){ 
       selector.jScroller("moveRight"); 
      } 
     }, settings.time); 

     $this.data('jScroller', { 
      settings: settings, 
      element: this 
     }); 
    }); 
} 
$.fn.jScroller = function(call){ 
    if (jScrollerMethods[call]) { 
     return jScrollerMethods[call].apply(this, Array.prototype.slice.call(arguments, 1)); 
    } else if (typeof call === 'object' || ! call) { 
     if (call) { $.extend(settings, call); } 
     return jScrollerMethods.init.apply(this, call); 
    } else { 
     $.error('Method ' + call + ' does not exist on jQuery.jScroller'); 
    } 
} 
相關問題