<script> window.onload = function () { var timer = 8; function f() { alert(timer); } var int = setInterval (function () { document.body.innerHTML = timer; timer--; if (timer == 0) { f(); clearInterval(int); } }, 1000); } </script>