Skip to content

Instantly share code, notes, and snippets.

@s749312025
Last active January 29, 2021 10:25
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 s749312025/323828043b12b313a1edbfb431b59cf9 to your computer and use it in GitHub Desktop.
Save s749312025/323828043b12b313a1edbfb431b59cf9 to your computer and use it in GitHub Desktop.
页面彩蛋
function egg(arr, callback) {
this.sequence = arr.join(',')
this.length = arr.length
this.pressed = []
this.callback = callback
this.init()
}
egg.prototype = {
constructor: egg,
init: function () {
document.addEventListener('keyup', this.handleKey.bind(this), false)
},
handleKey: function (e) {
this.pressed.push(e.keyCode)
console.log(this.pressed)
this.check()
},
check: function () {
if (this.pressed.join(',') == this.sequence) {
this.callback()
this.pressed = []
} else if (this.pressed.length == this.length) {
this.pressed.shift()
}
},
}
new egg([38, 40, 38, 40], function () {
alert('go!')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment