我有这个功能,我需要它在到达终点后从头开始重复。
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)
}
})
}
一旦它到达终点,我怎样才能让它循环?
最佳答案
一般来说,你想使用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
}
}
关于javascript - 如何重复这个功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45285829/