Skip to content

Instantly share code, notes, and snippets.

@nicoespeon
Last active March 21, 2017 08:38
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 nicoespeon/9af155c5a208f64dce0b11eddc01d1fe to your computer and use it in GitHub Desktop.
Save nicoespeon/9af155c5a208f64dce0b11eddc01d1fe to your computer and use it in GitHub Desktop.
Blog - Using Observables to make our app work with barcode scanners
const ENTER_KEY_CODE = 13
const MAX_INTERVAL_BETWEEN_EVENTS_IN_MS = 50
let keyCodesBuffer = []
document.addEventListener("keypress", (event) => {
const keyCode = event.keyCode
if(keyCode === ENTER_KEY_CODE) {
fillInputWithKeyCodesBuffer()
cleanBuffer()
} else {
addToBuffer(keyCode)
cleanBufferAfter(MAX_INTERVAL_BETWEEN_EVENTS_IN_MS)
}
})
function fillInputWithKeyCodesBuffer() {
// …
}
function cleanBuffer() {
keyCodesBuffer = []
}
function addToBuffer(keyCode) {
keyCodesBuffer.push(keyCode)
}
function cleanBufferAfter(timeout) {
setTimeout(cleanBuffer, timeout)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment