...

View Full Version : Javascript countdown (disabling submit button)



sys
08-07-2012, 08:28 AM
Hey there,
I'm currently making a login form for my site and I want to make a small protection. If someone tries to login with false login information the submit button has to disable itself for 5 seconds and add a timer to the submit button.
I'm doing it with PHP and I have done everything except the javascript part.
I found this script, but I can't make it show as a prefix in the submit button and make it enabled when timer has reached 0.



<script type = "text/javascript">
/*author Philip M. 2010*/

var timeInSecs;
var ticker;

function startTimer(secs){
timeInSecs = parseInt(secs)-1;
ticker = setInterval("tick()",1000); // every second
}

function tick() {
var secs = timeInSecs;
if (secs>0) {
timeInSecs--;
}
else {
clearInterval(ticker); // stop counting at zero
// startTimer(60); // remove forward slashes in front of startTimer to repeat if required
}

document.getElementById("countdown").innerHTML = secs;
}

startTimer(30);

</script>


Sorry for my bad english. If you don't understand something just ask and I'll try to say it with other words.

Regards,Sys.

Philip M
08-07-2012, 08:55 AM
<html>
<head>
</head>
<body>

<span id = "countdown"></span>
<input type = "submit" id = "subbutton" value = "Submit Disabled" disabled>

<script type = "text/javascript">
var timeInSecs;
var ticker;

function startTimer(secs){
timeInSecs = parseInt(secs);
tick(); // start timer right away
ticker = setInterval("tick()",1000); // every second
}

function tick() {
var secs = timeInSecs;
if (secs>0) {
timeInSecs--;
}
else {
clearInterval(ticker); // stop counting at zero
document.getElementById("subbutton").disabled = false;
document.getElementById("subbutton").value = "Submit Enabled";

}

document.getElementById("countdown").innerHTML = secs;
}

startTimer(5);

</script>

</body>
</html>


"In Germany they came first for the Communists, and I didn't speak up because I wasn't a Communist.
Then they came for the Jews, and I didn't speak up because I wasn't a Jew. Then they came for the trade unionists,
and I didn't speak up because I wasn't a trade unionist. Then they came for the Catholics, and I didn't speak up
because I was a Protestant. Then they came for me, and by that time there was no one left to speak up." -
Martin Niemöller, German Lutheran Pastor.

sys
08-07-2012, 09:07 AM
Thanks! Awesome, but can you make the countdown directly in the submit button? And the zero to disappear when it reaches 0?

Philip M
08-07-2012, 09:17 AM
Thanks! Awesome, but can you make the countdown directly in the submit button? And the zero to disappear when it reaches 0?

Surely you can do this simple adjustment yourself? Just move this one line and replace countdown by subbutton:-


if (secs>0) {
timeInSecs--;
document.getElementById("subbutton").value = secs;
}
Obviously the <span id = "countdown"></span> is no longer required.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum