Ever have some text that you wanted a person to read before they submitted content to your site. Most of the time, the people just scroll to the bottom of the page and click the submit button. In the Web 2.0, people have added additional code to make sure that the person has read the text. I am for sure most of you have seen the sites where you have to scroll an entire div block in order to have a button enabled that you can then click to say you agree to something. In this tutorial we will do a similar delay, but this will be a delay that you have more control over than the user.
Now to actually create the countdown, we will need to is create a function that can be loop and take the value away from the tminus variable. Let’s call this function countdown, since that is what it will be doing.
Now that we have our function, let’s actually make it do something. Let’s have the function subtract 0.1 from value of tminus.
Now, everytime the function is ran, it will reduce 0.1 from the value of tminus. The only issue with this, is that we can’t stop the timer at 0.0. To fix this issue we need to create an if statement that will tell the timer to stop when tminus gets to 0.1. You maybe asking why 0.1, well if we tell the timer to stop when tminus gets to 0.0, the function will execute one extra time, thus making the countdown go to 0.09, which we don’t want to happen.
When tminus value is zero we need to change the value of the button to Submit. To do that we will need to add an else statement to our function.
By adding the else statement, we are changing the value of tminus to something completely different if it is less then or equal to 0.1. In this case, we are changing the value to the word I Agree, since we want to user click the button if they agree to the rules. There are just a few more things we have to add to the script to toggle the button, and to change the value of the button while the user is waiting.
This part of the code, will still be inside the function. It will allow us to check the value of tminus, and if tminus does not match the string (I Agree), then the element named countdown in the countdown_forum will be disabled. Once the value of tminus is set to I Agree, then the button will be enabled. Now for the part, of the code that makes the count down actually work.