Hello folks a friend here in the forum helped me make a button and I thank him so much, it worked perfectly. But I failed to put cookies and I'm not understanding where I'm wrong (I was following a video on youtube to do it)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Contador</title>
</head>
<body>
<script language="JavaScript">
function dispara( span ) {
conta( span, document.getElementsByClassName(span)[0]);
}
function conta( botao, contador ) {
document.getElementById(botao).disabled=true;
if(validaCookie()){
contador.innerHTML = contador.innerHTML -1;
criarCookie(contador.innerHTML);
}
else{
contador.innerHTML = 60;
criarCookie(60);
}
if(contador.innerHTML <= 0) {
document.getElementById(botao).disabled=false;
invalidarCookie();
return false;
}
setTimeout( function(){conta( botao, contador )}, 1000 );
}
</script>
<input type="button" value="botao1" onclick="dispara('s1')" id="s1" class="btn">
<span class="s1" id="sp1">60</span>
<script type="text/javascript">
if(validaCookie()){
document.getElementById("sp1").innerHTML = lendoCookie();
}
//Criando o Cookie
function criarCookie(valorCookie){
//Criar objeto Date
var data = new Date()
//Setando o tempo de vida do Cookie
data.setTime(data.getTime() + 600000);
//Criando a estrutura do Cookie
document.cookie = "cookieCount="+valorCookie+"; expires="+
data.toUTCString()+"; path=/";
}
//Validando a existência do Cookie
function validaCookie(){
if(document.cookie.indexOf("cookieCount") == (-1)){
return false;
}else{
return true;
}
}
//Lendo o conteúdo do Cookie
function lendoCookie(){
//document.cookie retorna chave=valor
var valor = document.cookie.split("=");
return valor[1];
}
//Invalidando o Cookie
function invalidarCookie() {
document.cookie = "cookieCount=0; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/";
alert("Cookie invalidado!");
}
</script>
</body>
</html>