Friday, July 25, 2008

Government waste

Picking up on an idea by Tim Worstall, I put a quick javascript widget together yesterday to show the amount spent by the US government on its stupid War Against Drugs since the start of this year. You can see it in the sidebar here, and if anyone wants to use it the code is as follows:

<div id="drugCounter">
<div id="caption">Money spent by the US Federal Government since the start of this year:</div>
<div id="amount"></div>


<script type="text/javascript">
var annualSpend = 39900000000;
var daysInYear = 365
var hoursInDay = 24;
var minutesInHour = 60;
var secondsInMinute = 60;

var secondsInYear = daysInYear * hoursInDay * minutesInHour * secondsInMinute;
var millisecondsInYear = secondsInYear * 1000;

var spendSoFar = 0;
var spendPerSecond = annualSpend / secondsInYear;

var counter = document.getElementById("amount");

String.prototype.reverse =
function() { return this.split('').reverse().join(''); } = function()
var s = parseInt(this).toString().reverse(), r = '';
for (var i = 0; i < s.length; i++)
r += (i > 0 && i % 3 == 0 ? ',' : '') + s.charAt(i);

return r.reverse();

function calculateSpend() {
var d = new Date();
var t = d.getTime();
var y = t/millisecondsInYear;
var roundY = parseInt(y);

var secondsThisYear = parseInt((y - roundY) * secondsInYear);
spendSoFar = parseInt(secondsThisYear * spendPerSecond);
spendSoFar =;

counter.innerHTML = "$ " + spendSoFar;

setInterval("calculateSpend()", 2000);
Of course, if the caption is changed and the spend amount in bold red above altered appropriately, it will count the amount spent on anything this year, which is why I wrote it. While the drug spending waste is important, I have been meaning to do one of these things for total UK government waste, and another for EU waste. The amount given to the EU by UK taxpayers is another possibility.

This bit of script was slung together quickly (I nicked the number formatting stuff from here) but plan to rewrite it this weekend for some of these other purposes. I'm thinking of a configurable widget for displaying a number of different types of spending in one compact unit.

Suggestions for numbers to include, together with sources, are welcome. My source for US Federal drug spending is here.

My script above is free of copyright restrictions - do whatever you like with it. That might or might not be true of the number formatting snippet but the link is above if you want to be sure. I'll rewrite it all to make it completely free for unrestricted use this weekend.

UPDATE: Further to Mark's comment, the things to change for your own use are now in red. I also stripped out a couple of redundant variables - as I said, this was done in a rush. I'll make a nice config section at the top when I rewrite it over the weekend.

UPDATE 2 If you want to use the GBP symbol instead of the dollar symbol, it's better to type "&pound;" (excluding quotes) than the symbol on your keyboard.

UPDATE 3: Some duplication removed.


Mark Wadsworth said...

That is brilliant. Pls confirm there are two basic variables "what we spend it on" and "how much". So I could use it for my annual spend on fags and booze, for example?

Nick M said...

How about the 2012 Olympics?

Peter Risdon said...

Nick, great idea.

Mark Wadsworth said...

Ta muchly.

Peter Risdon said...

Most welcome.