2011-01-19 60 views
3

當在init();中調用iKeyless.widget.Rotator.rotate();函數時,我的旋轉橫幅腳本拋出「太多遞歸」錯誤。我想不通爲什麼,init();只調用一次,有一個延遲()的第二個呼叫....當它扔「太多遞歸」我的旋轉橫幅腳本中的「遞歸太多」

return { 
     init: function() { 
      ui.container = $("#rotating-banner"); 
      ui.thumbs = $("#rotating-banner .tabs .tab"); 
      ui.banners = $("#rotating-banner .banner"); 
      ui.fadeBox = $(".rotate .bd .fade-box"); 

      ui.thumbs.each(function (idx, el) { 
       $(el).click(function() { 
        paused = true; 

        iKeyless.widget.Rotator.rotate.show(idx); 
       }); 
      }); 

      iKeyless.widget.Rotator.rotate(); 
     }, 
     show: function (idx) { 
      ui.fadeBox.css("display", "block"); 
      ui.fadeBox.animate({ 
       opacity: 1 
      }, 
      .125, 
      function() { 
       $('.active', $(ui.banners[idx]).addClass('active').parent()).removeClass('active'); 

       ui.fadeBox.animate({ 
        opacity: 1 
       }, 
       .125, 
       function() { 
        ui.fadeBox.css("display", "none"); 
       }); 
      }); 
     }, 
     rotate: function() { 
      if (!paused) { 
       this.show(counter); 

       counter = counter + 1; 

       if (counter === ui.thumbs.length) { 
        counter = 0; 
       } 

       iKeyless.widget.Rotator.rotate().defer(5000); 
      } 
     } 
    } 
+0

Tagwise - 這是原型而不是jQuery? – 2011-01-19 16:38:22

回答

1

我認爲這個問題是,你需要做到這一點

iKeyless.widget.Rotator.rotate.defer(5000); 

,而不是這個

iKeyless.widget.Rotator.rotate().defer(5000); 

Source:當你寫旋轉(),您執行的功能rotate,然後執行defer。當您寫入rotate.defer()時,您將獲得功能rotate,並使用Function上定義的方法defer

另外,如果這是jQuery,你如何定義延遲?我不確定jQuery提供了延遲功能。如果這是Prototype,我認爲延遲不會引發任何爭論。也許你想delay。延遲需要幾秒鐘的時間,所以你想(假設5秒,而不是5000秒):

iKeyless.widget.Rotator.rotate.delay(5); 
0

滯後之前加載頁面我沒有」知道延期,但我讀的是這樣的:

計劃函數運行,只要解釋器​​空閒。

但這並不意味着它實際上沒有這樣做。函數rotate會一次又一次地調用它自己。不是線性的,而是在其內部,所以你永遠不會結束'頂部'調用旋轉。

這將意味着無限遞歸,所以我對這個錯誤並不感到驚訝。

0

我對defer()不熟悉,但您在rotate方法中調用rotate(),這是遞歸的來源。也許利用諸如setTimeoutsetInterval之類的東西可以解決您的問題嗎?