Skip to content

Instantly share code, notes, and snippets.

@timblair
Created December 28, 2008 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timblair/40340 to your computer and use it in GitHub Desktop.
Save timblair/40340 to your computer and use it in GitHub Desktop.
Simple JavaScript in-page counter
var Counter = {
_c: {},
get: function(c) { return Counter._c[c] || 0; },
set: function(c, v) { Counter._c[c] = v; return v; },
inc: function(c, v) { return Counter.set(c, Counter.get(c) + (v || 1)); },
dec: function(c, v) { return Counter.inc(c, (v ? -v : -1)); }
};
/**
* Usage:
* Counter.get("x") -> 0
* Counter.inc("x") -> 1
* Counter.inc("x", 2) -> 3
* Counter.dec("x") -> 2
* Counter.set("x", 5) -> 5
* Counter.get("x") -> 5
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment