Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Jack Johnny

Desativar botão ao clicar

Recommended Posts

Eu tenho o seguinte botão:

 

<input type="button" value="Divulgar!" style="height:40px; width:200px; font-size:18px;" id="botaodivulgar" onfocus="e = document.getElementById( 'text' ); if( e.value == '' ) { e.focus(); }" onclick="c = 'divulgar.php?url=' + encodeURI( document.getElementById( 'url' ).value ) + '&tumblrtype=' + verifyRadio() + '&txt=' + encodeURI( document.getElementById( 'text' ).value ); carregaAjax( 'response', c ); var i = self.setInterval( function() { carregaAjax( 'mural', 'divulgacao.php' ); window.clearInterval(i); }, 3000 );" />

 

E essa parte:

 

var i = self.setInterval( function() { carregaAjax( 'mural', 'divulgacao.php' ); window.clearInterval(i); }, 3000 );

Faz com que seja carregada uma página meio pesada. Acontece que em 3 segundos a página recarrega, e ao clicar no botão infinitas vezes, a página fica carregando e recarregando e isso pode pesar o site.

 

O que eu gostaria de saber é se tem como, ao clicar no botão, desativá-lo e depois de 5 segundos reativá-lo novamente. É possível?

 

Eu estava tentando fazer o seguinte:

 

onclick="this.disabled = true; this.enabled = false; setInterval( function() { this.disabled = false; this.enabled = true; }, 5000 );"

Mas isso não funcionou.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Imagino que se você definir o tempo desse setInterval() dinamicamente, acrescendo 5 segundos ao tempo atual obtido na hora do clique seja possível.

 

Mas como não manjo muito de JavaScript fico só na teoria. :pinch:

Compartilhar este post


Link para o post
Compartilhar em outros sites

jQuery

$(function() {

$('#button').click(function(){
	$('#button').prop('disabled', true);
	window.setTimeout(function() {
		$('#button').prop('disabled', false);
	}, 5000);
});
			
});

 

exemplo input:

<input type="submit" value="jQuery" id="button" />

 

Javascript

function desativa(id){
	document.getElementById(id).disabled=true;
	setTimeout(function() {
		document.getElementById(id).disabled=false;
	}, 5000);
}

 

exemplo input:

<input type="submit" value="JS function" id="button2" onclick="desativa('button2');"/>

 

Ou....

<input type="submit" value="JS onClick" id="button3" onclick="this.disabled=true;setTimeout(function(){document.getElementById('button3').disabled=false;},5000);"/>

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.