There comes a time when we need a visual display of how many seconds are left till the popup window closes. This short code does just that. It displays how many seconds are left and closes the popup window when the number of seconds is zero.

The HTML code would look like this

The Javascript code uses the functions setTimeout and clearTimeout respectively. Take note that window.clearTimeout() must be included in order for this to work. Otherwise, when the page loads, it closes it instantly. I read about what clearTimeout() is for, but I could not grasp how that is of use to the code to make the countdown timer work.

Once the page finishes loading due to the body tag’s onLoad event, the timer calls the timer() function and starts the countdown.

The code above closes the window in 5 seconds. If you need it for a few seconds more, just add 1 to the number of seconds till it reaches zero to the variable seconds.

Related Posts Plugin for WordPress, Blogger...