Last active
August 29, 2015 14:02
-
-
Save kitak/202e2dea954b048fb6bf to your computer and use it in GitHub Desktop.
コナミコマンドを実装した
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 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