Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
How to reset recursive function in JS?

I have a function to follow the multistep form:

function multistep() {
        function disableBtn() {
            continueButton.attr('disabled', true);
        }
        let select = $('.cf7mls_current_fs select');
        let continueButton = $('.cf7mls_current_fs .cf7mls_next');
        $('.wpcf7-submit').attr('disabled', false);
        $('.preloader-form-wrap').hide();
        $('.select-choosen').each(function () {
            $(this).removeClass("select-choosen");
        });


        let selectArr = [];
        let currentArr = [];

        select.each(function (i, obj) {
            currentArr.push($(obj).attr("name"));
        })

        continueButton.attr('disabled', true);
        select.on('change', function () {
            if ($(this).prop('selectedIndex') !== 0) {
                if ($(this).hasClass("select-choosen") === false) {
                    $(this).addClass("select-choosen");
                    selectArr.push($(this).attr("name"));
                }
            } else {
                $(this).removeClass("select-choosen");
                selectArr.shift();
                disableBtn();
            }
            console.log(selectArr.length, currentArr.length);
            if (selectArr.length === currentArr.length) {
                continueButton.attr('disabled', false);
                $(continueButton).on("click", function () {
                    selectArr = [];
                    $('.preloader-form-wrap').show();


                    setTimeout(() => {
                        // if is everything ok, runs function again!
                        // but array from above will not be reset
                        multistep();
                    }, 1000);
                });
            }
        });
    }

And the goal of the function is to enable button if every step is filled, and this works, but I have a problem with

let selectArr = [];

When a function calls itself, the inner, variable from (array) above will not be reset? And function will be called multiple times.

First time call function:

$('.btn-open-form').click(function (e) {
....
multistep();
}

And reset again:

function closeForm() {
$(document).click(function (e) {
    if ((e.target.className === "overlay-mobile form-active") == true
            ) {
   multistep();
   } 
}

Function from above will be called on document init.

Well, let selectArr = []; if an array of choosing select items in one tab, and important is to have the length of the array above be equal to a number of displayed selected items. If the length is the same, the button will be enabled, to jump on another tab.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda