Skip to content

Instantly share code, notes, and snippets.

@kitak
Last active August 29, 2015 14:02
Show Gist options
  • Save kitak/202e2dea954b048fb6bf to your computer and use it in GitHub Desktop.
Save kitak/202e2dea954b048fb6bf to your computer and use it in GitHub Desktop.
コナミコマンドを実装した
var keyCodeCheck = (function () {
var konamiCommandKeyCodes = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];
var acceptIndex = 0;
var timerId = null;
var resetAcceptIndex = function () {
console.log('最初からやりなおしです');
acceptIndex = 0;
};
return function (e) {
if (e.keyCode == konamiCommandKeyCodes[acceptIndex]) {
clearTimeout(timerId);
acceptIndex += 1;
if (acceptIndex == konamiCommandKeyCodes.length) {
alert('コナミコマンドを受け付けました');
acceptIndex = 0;
} else {
timerId = setTimeout(resetAcceptIndex, 1000);
}
} else {
acceptIndex = 0;
}
};
})();
window.onload = function () {
document.body.addEventListener('keyup', keyCodeCheck);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment