如何重复此功能

如何重复此功能

问题描述:

我有这个功能,我需要它从头开始重复,一旦到达结尾。如何重复此功能

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

一般来说,你想使用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