Created
September 15, 2011 17:22
-
-
Save oranj/1219874 to your computer and use it in GitHub Desktop.
Konami Code javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var konami_goal = '38 38 40 40 37 39 37 39 66 65 13'; | |
var konami_text = ''; | |
var konami_interval; | |
var konami_callbacks = { | |
'update':Array(), | |
'success':Array(), | |
'failure':Array(), | |
'timeout':Array(), | |
}; | |
function konami_setup() { | |
$(document).keyup(function (e) { | |
if (konami_text) { | |
konami_text += ' '; | |
} | |
konami_text += e.keyCode; | |
clearInterval(konami_interval); | |
konami_interval = setTimeout(konami_timeout, 1000); | |
konami_check(); | |
}); | |
} | |
function konami_check() { | |
var current_length = konami_text.length; | |
var substr = konami_goal.substring(0, current_length); | |
if (konami_text == konami_goal) { //matches | |
konami_trigger('success'); | |
clearInterval(konami_interval); | |
konami_clear(); | |
} else if (konami_text == substr) { // Is substring | |
konami_trigger('update'); | |
} else { | |
konami_trigger('failure'); | |
konami_clear(); | |
} | |
} | |
function konami_timeout() { | |
konami_trigger('timeout'); | |
konami_clear(); | |
} | |
function konami_clear() { | |
konami_text = ''; | |
} | |
function konami_add_callback(_callback, _event) { | |
konami_callbacks[_event].push(_callback); | |
} | |
function konami_trigger(_event) { | |
for (var i in konami_callbacks[_event]) { | |
if (konami_callbacks[_event].hasOwnProperty(i)) { | |
konami_callbacks[_event][i](); | |
} | |
} | |
} | |
$(konami_setup); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment