First, the objects that will be responsible for manipulating the DOM and the word list:
const form = document.getElementById("procurar-form");
const input = form.getElementsByTagName("input")[0];
const button = document.getElementById("button_procurar");
const palavras = ["Papel", "Vidro", "Lata", "Terra", "etc"];
Second, we need to write a function that generates a random value, update the word within the field and execute the click of the button (the latter was prompted in the comments). To do so, we do:
function novaPalavra() {
// Gera um valor aleatório de 0 ao comprimento da lista de palavras:
const index = Math.floor(Math.random() * palavras.length);
// Atualiza o valor do campo para a palavra sorteada:
input.value = palavras[index];
// Executa o clique do botão:
button.click();
}
This function does what you want, but you have also been asked to run this code every 5 minutes. To do this, we can use the function setInterval
, which takes the name of a function and the range it will be executed in milliseconds. For a 5 minute break, we need an interval of 5*60*1000 milliseconds. So:
setInterval(novaPalavra, 5*60*1000);
This way, every 5 minutes the word in the field will be changed to another one randomly and a click on the button will be executed.
See working:
const form = document.getElementById("procurar-form");
const input = form.getElementsByTagName("input")[0];
const button = document.getElementById("button_procurar");
const palavras = ["Papel", "Vidro", "Lata", "Terra", "etc"];
function novaPalavra() {
const index = Math.floor(Math.random() * palavras.length);
input.value = palavras[index];
button.click();
}
setInterval(novaPalavra, 1000);
<form id="procurar-form" method="get" action="Site">
<input type="text" class="procurar-field" placeholder="Vamos Procurar?" name="q" value="verde">
<button class="procurar-btn" id="button_procurar">Procurar</button>
</form>
Since the click of the button will submit the form, updating the page - or redirecting the user - makes no sense to have a code that runs every 5 minutes, because always in its first execution the same will stop due to the submission of the form. The reply was given in this way by the insistence of the author on the comments.
Dmelo, take a look here: https://answall.com/a/64022/129 is what you’re looking for but with longer time intervals?
– Sergio
Thank you very much for the answer, but it’s not what I need. As I said, it’s a lot of words and my code doesn’t have an id for javascript. Time is no problem. If it is possible put the complete javascript code to see if it works.
– DMelo
Where are these various words? What do you mean by "inserted in sequence"? Each new word will be added to
input
without deleting old ones or each word will appear alone for 5 minutes?– Woss
These are random words. For example: Tin, Glass, Paper, Earth, etc. All words I have in a txt file, but if it is difficult javascript to get txt the words can be in the javascript itself. In sequence means one at a time inserted into the input alone every 5 minutes.
– DMelo
In fact Javascript does not have access to the file and must be directly in the code.
– Woss