2017-07-24 97 views
1

我有這個功能,我需要它從頭開始重複,一旦到達結尾。如何重複此功能

processSlider: function() { 
    function a() { 
     var c = document.querySelector(".layout-process-slider .process-point.active"); 
     if (!c.nextElementSibling) { 
      $(".layout-process-slider .process-point").first().trigger("click") 
     } else { 
      $(".layout-process-slider .process-point.active").next().trigger("click") 
     } 
    } 
    if ($(".layout-process-slider").length) { 
     var b = setInterval(a, 2500) 
    } 
    $(".layout-process-slider .process-point").click(function(d) { 
     var c = $(this).data("select"); 
     $(this).siblings().removeClass("active"); 
     $(this).addClass("active"); 
     $(this).parent().parent().find(".items").children().removeClass("active"); 
     $(this).parent().parent().find('.item-info[data-item="' + c + '"]').addClass("active"); 
     if (d.originalEvent !== undefined) { 
      window.clearInterval(b) 
     } 
    }) 
} 

我怎樣才能得到這個循環,一旦它達到結束?

+1

當使用遞歸記得要保持以完成執行的條件,否則你一定會最終掛您的瀏覽器 – Bardo

回答

2

一般來說,你想使用recursion這個。

給你的函數一個名字,並在它到達結尾時調用它自己。

function f() { 
// ... 
f(); // <-- recurse 
} 

所以你的情況,像這樣的工作:

processSlider: function processSlider() { 
    //     ^give it a name (doesn't have to be the same as the property to the left) 
    // ... 
    processSlider(); // <-- recurse 
} 

一個重要的警告是要確保你有一個遞歸基本情況將無限期地停止運行的功能(除非你真的希望它永遠運行)。

processSlider: function processSlider() { 
    // ... 

    if (! /* whatever your base case is */) { 
    processSlider(); // <-- recurse only if base case is false 
    } 
} 
+0

我得到'未捕獲的RangeError:使用例如最大時調用堆棧大小exceeded'。 – jarmerson

+0

@jarmerson,因爲您沒有遞歸基礎案例。您需要一些邏輯來確定何時停止運行遞歸。 – nem035

+0

這是我正在處理的頁面 - https://sites.btwebnetwork.com/compliancemate/compliancemate/#child-75 – jarmerson