Creating a Countdown

Sooner or later, you’ll want to put a countdown on your pages that tells the user how many days or hours until a particular event. Script 12.8 (HTML) and Script 12.9 (JavaScript) lets one of the authors know his responsibilities, in no uncertain terms, as you can see in Figure 12.6.

Figure 12.6. Loading this page gives one of the authors his marching orders.

To create a countdown:

1.
var allTags = document.getElementsByTagName("*");
Create a new allTags array, and fill it with every tag on the page.
2.
for (var i=0;i<allTags.length; i++) { if (allTags[i].className.indexOf("daysTill") > -1) { allTags[i].innerHTML = showTheDaysTill(allTags[i].id); ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.