Akxl Labs C# ASP.NET Articles and Tutorials Akxl Labs Web Apps and Tools for Your Website

The Coffee Counter

Tags tagged as   Akxl: Website  Code: JavaScript, Web  Life: Coffee
How much coffee does a college student need?

Posted October 07, 2006    Viewed 2902 times    Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Summary

Update: I've added a coffee counter generator. Create your own coffee counter, and add it to your website with the given code.



Everyone always asks me how the coffee counter on the site works. Here's what the calculation is based on:

I spent the entire month of January 2005 keeping count of every cup of coffee I drank. Using this count, I came up with a daily average.

This average was then broken down into an average per second. So I calculate the seconds elapsed sine the start of the tracking, and then multiply this by some constants. The first constant represents the number of milliliters per second. This is found by converting the volume of the coffee cup I use into milliliters (we did this in the kitchen in East Hall, with a measuring cup, a laptop, and Google).

Then, we derive the milligrams of caffeine based on the average for a cup of African tropical coffee (my normal drink), with a little extra weighting, since I buy the thickest kind and brew it as dark as possible. Once I had a constant for the milligrams / milliliter, I calculated a constant for milligrams per second.

So the entire calculation is based on multiplying seconds elapsed by two constants. It's all very scientific, I assure you.

Articles and downloads sponsored by:
Thanks! Amazon commissions help me pay for textbooks.

Source Code

1 var coffeeDataArea = "spanCoffeeCounter"; 2 3 function CalculateCoffee() 4 { 5 // -- Get the seconds since Jan 1, 1960 6 var date = new Date(); 7 var secondsElapsed = Math.floor(date.getTime()/1000); 8 9 // -- Subtract the seconds between Jan 1, 1960 and Jan 1, 2005 10 // -- to get the seconds between now and Jan 1, 2005 11 // -- (the start of tracking) 12 secondsElapsed -= 1104641740; 13 14 // -- Calculate out the current totals based on the tested 15 // -- average amount of coffee, converted to coffee/second, 16 // -- and using the average caffination of African tropical 17 // -- coffee with weighting for expresso grinding and 18 // -- bioling-down 19 var mL = secondsElapsed * 0.0095393518; // milliliters 20 var mg = secondsElapsed * 0.0088738425; // milligrams of 21 // caffeine in the 22 // above mL of coffee 23 24 // -- Find the number of cups by converting from milliliters 25 var cups = mL * 0.00422675282; 26 27 // -- Build the counter HTML 28 var html = ""; 29 30 html += " Since 2005: " + cups.toFixed(1) + " cups of coffee, " 31 + mg.toFixed(2) + " mg of caffeine. "; 32 33 // -- Display the counter HTML 34 try 35 { 36 document.getElementById(coffeeDataArea).innerHTML = html; 37 } 38 catch (exception) 39 { 40 // Span not loaded yet, the function started before 41 // the page loaded, just go around again and we'll 42 // get it next time. This way, we avoid 43 // having to use window.onload, and all those issues. 44 } 45 } 46 47 // -- Start it up 48 setInterval("CalculateCoffee()", 1000);

Comments & Feedback


There are no comments on this entry.
Leave this field blank:
Comment on this Entry
This work is licensed under a Creative Commons Attribution 3.0 United States License.
Please link to this article in your source code comments if you use this content.

Labs

Blog

The blog has moved.
Non-technical articles are now on a seperate site.
Contact me for the new address.

Apps

Real-Time Coffee Counter
add it to your website!
Golden Ratio Visualizer
a tool for design

Coffee Counter

Current Count:
Akxl Coffee Meter
Current Coffee:
 Peet's Malawi Songwe River

The Real-Time Coffee Meter is a free Website App from Akxl Labs. Text-only and badge versions available.