Having more than half of the decade of JavaScript programming experience I never used ‘sleep’ functionality in JavaScript until recently.
To my surprise there is no such function in JavaScript, for comparison there is sleep function in PHP and threads in Java.
I was even more surprise when I didn’t find it in jQuery, there are lots of countdown plugins though.
All I needed to do is a simple countdown from 10 to 0 second with redirect which is intuitive to do with a loop and sleep(number of seconds) algorithm.
Luckily JavaScript has window.setTimeout function and I was able to use it in recursive function with condition.
Here is my code with no jQuery used:
var i=10; Countdown(); function Countdown() { document.getElementById('countdown').innerHTML=i; if (i>0) { i--; window.setTimeout(function (){Countdown();},1000); } else { window.location="http://webapplog.com"; } }
Generally I don’t read post on blogs, however I would like to say that this write-up very pressured me to try and do so! Your writing style has been amazed me. Thank you, quite nice article.