-->

Javascript: Close Popup Window In X Seconds

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.

?View Code JAVASCRIPT
1
<script type="text/javascript"><!--mce:0--></script>

The HTML code would look like this

?View Code HTML4STRICT
1
2
 
Window will close in <span id="time">5</span> second(s)

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.

?View Code HTML4STRICT
1
 

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.

Found this post useful? Donations appreciated. Every little $ helps.

Related Posts with Thumbnails

One Response to “Javascript: Close Popup Window In X Seconds”

  1. 1
    North Sulawesi Says:

    what a great info!!anyway,thx for visiting my
    North Sulawesi
    blog

Leave a Reply

Spam protection by WP Captcha-Free