How to generate number without repetition with a pause button?

Asked

Viewed 286 times

1

I would like you to have a button with the function of PAUSE, and eliminate duplicate numbers.

A down counter that when I press the PAUSE it displays the result number with the TOTAL of generated lines .

function append(el) {
    //eu coloco os 16 primeiro digitos aqui, e ele gera mais 4 aleatorio
    var txt = "1212" + "9057" + "9240" + "8105";
    var x = Math.floor((Math.random() * 9000) + 1000);
    document.getElementById(el).innerHTML += txt + x + "<br>";
    //tempo de gerar numeros 1s
    setTimeout(function() {
        append("timeout");
    }, 1000);
    //AQUI EU QUERO UM FILTRA PARA NAO DUPLICAR
    // E UM BOTAO DE PAUSE E UM TXT COM TOTAL DE LINHAS GERADAS

}


append("timeout");
<p>GERADOR DE NUMEROS SEM REPETIÇAO E COM BOTAO DE PAUSE</p>
<hr>
<button name="pause" id="pause">PAUSE</button>
<div id="timeout"></div>
<div id="interval"></div>

  • Got it, you want to pause the number generator as soon as you click the pause button, correct?

  • YES , pause it when I want, and at the end of the pause or the result it shows me the total number generated ('Total results generated --> 5040 ') [EXAMPLE] at the end of each pause it presents me the total, and if it has how to add not to duplicate tbm would be great, but my focus is more on having a PAUSE button and a total count of results to be displayed at the end

  • I will add an answer, and I will edit to adjust your need.

2 answers

1

What I would do is this:

First use setInterval instead of setTimeout:

var myVar = setInterval(function(){ gerarNumero() }, 1000);

When you want to stop, use clearInterval:

clearInterval(myVar);

The case of duplications is more difficult, I recommend to make an array and put each generated number in it, and then check if the new number is in the array, if it is not, return the number, if it is running the function again...

var numeros = [];

function gerarNumero(){    
    var x = Math.floor((Math.random() * 9000) + 1000);
    var existe = false;
    for(var i in numeros){
        if(numeros[i] == x) existe = true;
        break;
    }    
    if(!existe){
        numeros.push(x);
        document.getElementById(el).innerHTML += txt + x + "<br>";
    }else{
        gerarNumero();
    }
}

I didn’t test the code, but it’ll be something along those lines...

I hope it helps...

  • Just remembering that at some point this code will run infinitely because there will be no new number and he will be calling himself forever... need to treat this case...

  • And I want a final result counter, when I click on PAUSE (or when it finishes all combinations), it goes from a break; and sends an innerHTML saying to me "Total line result ->" then appears the count of how many different results gave( without duplicates ),in case it will generate the final result of 4 digits combined different (0-9) is basically what I wish to accomplish, Thanks msm so for the attention

  • for example 4 digits generated are , 5040 combinations, I want at the end of it perform all, he sent me an innerHTML written "TOTAL RESULT GENERATED-> 5040 " so my interest in the counter too... if I want to pause the result, it shows me the total generated, I think I’ve now managed to express my idea ^^

0


The setInterval Returns a range ID, and can be passed as a parameter in the function clearInterval(), if you want to stop your execution.

See working:

var stop;
var contador = 0;
function append(el) {
    contador++;
    var txt = "1212" + "9057" + "9240" + "8105";
    var x = Math.floor((Math.random() * 9000) + 1000);
    document.getElementById(el).innerHTML += txt + x + "<br>";

    stop = setTimeout(function() {
        append("timeout");
    }, 1000);


}
function pause(){
   document.getElementById('totalItens').innerHTML = 'Total de Itens:' + contador;
   clearInterval(stop);
}
append("timeout");
<p>GERADOR DE NUMEROS SEM REPETIÇAO E COM BOTAO DE PAUSE</p>
<hr>
<button name="pause" id="pause" onclick='pause()'>PAUSE</button>
<div id="timeout"></div>
<div id="interval"></div>
<div id="totalItens"></div>

  • very good, that was right, if I want to put an accountant I should open another question ? and Accept this ? or I proceed right here...

  • @Mandrakevenom I added the total of items that were generated.

  • Okay, I’ve accepted the answer, Valew guy already gave me understand more about javascript and the functions of Interval, timeout and clear, vlw by the quick response

  • @Mandrakevenom good that I could help :). I recommend reading on tour to understand a little more about the site.

Browser other questions tagged

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