-1
I wish the following loading indicator : <li Carregando...</li>
appears on screen instead of a list of elements while the request is being performed. This is my code :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UFT-8">
<title> </title>
</head>
<body>
<form id="form-usuario" onsubmit="searchUsuario()" method="POST">
<input type="text" name="user" id="usuario" placeholder="Informe o usuario do github">
<button type="submit">Buscar usuário</button>
</form>
<div>
<ul id = lista>
</ul>
</div>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
//função para procurar o usuário
function searchUsuario() {
alert("searching...")
event.preventDefault()
let coleta = document.getElementById("usuario").value
axios.get(`https://api.github.com/users/${coleta}/repos`)
.then(function(response) {//caso o usuário seja encontrado...
console.log(response)
var nomes = []//vetor que armazenará os repositórios
nomes.push(`https://api.github.com/users/${coleta}/repos`)//adiciona o usuário obtido na busca/pesquisa do usuário
for (var i = 0; i<nomes.length; i++){
var lis = document.createElement('li')
lis.innerHTML = nomes[i]
lista.appendChild(lis)
}
console.log(nomes)
})
.catch(function(error) {
console.log(error)
console.log("Este usuário do github não existe")
})
}
</script>
</body>
</html>
How to solve this?
The only however of this logic is that the names vector empties, making the list no longer have content at each insertion.
– Gabriel Ribeiro Carneiro
Amended answer.
– Sam