Konami 01

Javascript

Public Domain

Konami cheat code

Download (right click, save as, rename as appropriate)

Embed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Fires "lol:konami" event on the document when the cheat is entered.
// NOTE: This version fires after the konami cheat sequence is detected (w/o ENTER)
document.observe('keyup', (function(element){
  var cache = [], code = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65], checkTimer, clearTimer, i;
  
  function clearCache() {
    cache = [];
  }
  
  function checkCache() {
    // Set inactivity timeout
    if (clearTimer) clearTimeout(clearTimer);
    clearTimer = setTimeout(clearCache, 500);
    
    // Check code
    if (cache.length != code.length) return;
    i = cache.length;
    while (--i) {
      if (code[i] != cache[i]) return;
    }
    clearTimeout(clearTimer);
    clearCache();
    element.fire("lol:konami");
  }
  
  // Observer
  return function(event) {
    // Check cache
    cache.push(event.keyCode ? event.keyCode : event.charCode);
    
    // Check when done typing
    if (clearTimer) clearTimeout(clearTimer);
    if (checkTimer) clearTimeout(checkTimer);
    checkTimer = setTimeout(checkCache, 500);
  };
})(document));