Hey, Javascript is not my strongest point, so I was hoping to get some help form y'all I'm working on a script that requires a countdown timer. Nothing fancy, just something that will show the numbers on the screen as it counts down (not in a textbox though, just normal numbers). When it hits zero, it should trip a seperate function. Please keep the code easily readable so I can make little changes as needed. Thanks for your time -Alex
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" > <head> <title>count down from 15</title> <script type="text/javascript"> window.onload = function() { /* set your parameters( number to countdown from, pause between counts in milliseconds, function to execute when finished ) */ startCountDown(15, 1000, myFunction); } function startCountDown(i, p, f) { // store parameters var pause = p; var fn = f; // make reference to div var countDownObj = document.getElementById("countDown"); if (countDownObj == null) { // error alert("div not found, check your id"); // bail return; } countDownObj.count = function(i) { // write out count countDownObj.innerHTML = i; if (i == 0) { // execute function fn(); // stop return; } setTimeout(function() { // repeat countDownObj.count(i - 1); }, pause ); } // set it going countDownObj.count(i); } function myFunction() { alert("hi alex"); } </script> </head> <body> <div id="countDown"></div> </body> </html>
"Awesome, that pretty much does it all. And I like the ending Thanks for the help." Hey, no problem. BTW, the line 'countDownObj.innerHTML = i;' would be more neatly expressed as 'this.innerHTML = i; "how do you know this." Many years as a web developer.