How do I count a javascript counter that is run by a function starting from the last number that stopped?

Asked

Viewed 203 times

-1

staff need to do a counter that is run by a function in javascript, but this counter when the function is to be run again should start from where it stopped example:

1
2 
3
4 
5 
6

terminou de executar a função.

I ran the function again

7  
8  
9  
10

would need to do something like this in javascript someone can help me follow the code I’m using, detail as you can see in the code when returning from ajax the page redirects.

let numeros = [];
let max = 100000;

//cadastra as informações selecionadas pelo cliente
function cadastrar(){
    //variaveis com os valores dos campos digitados pelo usuário
    var action = "cad_cliente";
    var ok = "#btnOK";
    var clean = "#btnClean";
    var radio = ":checkbox";
    var msgbox =".tab span";
    var msgbox1 =".conf span";
    var color;  

    var n = Math.ceil(Math.random() * max);
        while(numeros.indexOf(n) >= 0){
            n = Math.ceil(Math.random() * max);
        }
        numeros.push(n);

    if($(radio).is(":checked")){
    var pacote = document.getElementsByClassName('data');

        for (var i = 0; i < pacote.length; i++){

            if ( pacote[i].checked ) {
                var valores = pacote[i].value;

                var j = valores.split(';');
                var pedido  = $("#pedido_"+j).val();

                if(pedido == 0){
                    $(msgbox)
                        .html("A QUANTIDADE DO PEDIDO NÃO PODE SER 0")
                        .removeClass()
                        .addClass("alert alert-danger")
                        .show();
                    return false;
                } 
                var cod_cli = $("#cod_cli").val();
                var email   = $("#email_cli").val();
                var nome    = $("#nome_cli").val();
                var cnpj    = $("#cnpj_cli").val();
                var end     = $("#end_cli").val();
                var bairro  = $("#bairro").val();
                var numero  = $("#numero").val();
                var cep     = $("#cep_cli").val();
                var cidade  = $("#cid_cli").val();
                var sub = nome.substr(0,6);
                var replace = sub.replace(" ","");

                var num = replace+""+n;

                document.getElementById('cadastro').disabled = true;
                document.getElementById('cadastro').value = "Aguardando...";

                //manda a requisição dos campos para a pagina de controller
                $.ajax({
                    type: "POST",
                    url: "cliente_functionAG.php",
                    data: {
                        action: action,
                        valores: valores,
                        cod_cli: cod_cli,
                        email: email,
                        nome: nome,
                        cnpj: cnpj,
                        end: end,
                        bairro: bairro,
                        numero: numero,
                        cep: cep,
                        n:n,
                        cidade: cidade,
                        pedido: pedido
                    },
                    success: function(result){      

                        if(result == "1"){
                            $(msgbox1)
                            .html("CADASTRADO COM SUCESSO "+num)
                            .removeClass()
                            .addClass("alert alert-success")
                            .show();
                            $('html,body').animate({ scrollTop: 0 }, 'slow');
                            $(".cad").val('');
                            $("#field").prop("checked", false);
                            setTimeout(function()
                                {
                                    location.href="indexAG.php";

                                }, 5000);


                        }else{
                            $(msgbox)
                                .html("ERRO AO CADASTRAR")
                                .removeClass()
                                .addClass("alert alert-danger")
                                .show();
                        }

                        document.getElementById('cadastro').disabled = false;
                        document.getElementById('cadastro').value = "GERAR PEDIDO";
                    }
                });     
            }
        }
        $(msgbox).hide();
    }else {
        $(msgbox)
        .html("NÃO FOI SELECIONADO NENHUMA OPÇÃO")
        .removeClass()
        .addClass("alert alert-danger")
        .show();    
    }

}
  • Where is this count made? I looked at the whole code and could not identify where.

  • would be the variable n, is actually a random number that comes, I would need to exchange this for a counter that does not repeat the number

  • It’s still confusing me. You want me to count +1 every time the function is executed?

  • this, I would like that if the last execution finished at 4 in the next continues counting in 5

1 answer

0

Maybe this will help you, if you can bring more information.

var numeros = [];

var n = localStorage.get('n') || 0;

function SomaUm() {
 n++;
 localStorage.set('n', n);

 return numeros.push(n);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.