Trying to make a timer delayed button
Posted: Wed Sep 02, 2015 1:19 am
				
				I'm not much cop at javascript. I found a script that makes a button take ten seconds before it can be clicked/submitted. But I can't seem to implement it.
I tried to see if I was implementing scripts right at all - that 'paragraph changed' thing does work though.
So I don't know what I'm doing wrong? Any help appreciated.
Side notes: The timer is simply there to help ensure ads get some time to load. It'll just be a submit button in the end, not a download link.
			I tried to see if I was implementing scripts right at all - that 'paragraph changed' thing does work though.
So I don't know what I'm doing wrong? Any help appreciated.
Side notes: The timer is simply there to help ensure ads get some time to load. It'll just be a submit button in the end, not a download link.
Code: Select all
<!DOCTYPE html>
<html>
<head>
<script>
var downloadButton = document.getElementById("download");
var counter = 10;
var newElement = document.createElement("p");
newElement.innerHTML = "You can download the file in 10 seconds.";
var id;
downloadButton.parentNode.replaceChild(newElement, downloadButton);
id = setInterval(function() {
    counter--;
    if(counter < 0) {
        newElement.parentNode.replaceChild(downloadButton, newElement);
        clearInterval(id);
    } else {
        newElement.innerHTML = "You can download the file in " + counter.toString() + " seconds.";
    }
}, 1000);
</script>
<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
<style>
body {background-color:lightgrey}
h1   {color:blue}
p    {color:green}
.button {
    background-color: #ddcccc;
    border: 1px solid black;
    color: black;
    font-family: Arial;
    font-size: small;
    text-decoration: none;
    padding: 3px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<a href="downloadFile.zip" id="download" class="button">Download the file...</a><p>
<p id="demo">A Paragraph.</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>