I'm trying to build a javascript countdown timer.
I need a visible timer that lets appear a button when it has been finished.
Can anyone help me to build it?
I'm trying to build a javascript countdown timer.
I need a visible timer that lets appear a button when it has been finished.
Can anyone help me to build it?
You can do this using setInterval.
Here's a basic demo.
var secondsRemaining = 3;
function updateTime() {
$("#secs").text(secondsRemaining);
}
updateTime();
var i = setInterval(function() {
secondsRemaining -= 1;
if (secondsRemaining > 0){
updateTime();
} else {
clearInterval(i);
$("#secs").html("<button id='myButton'>Click me!</button>")
}
}, 1000);
$("#secs").on("click", "#myButton", function(){
alert("Hello!");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="secs"></div>
If you want to read more about how this works, this is a pretty decent tutorial: http://www.sitepoint.com/build-javascript-countdown-timer-no-dependencies/