欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)掃碼槍掃描二維碼功能

 更新時(shí)間:2020年01月03日 09:49:04   作者:葉為花而生  
這篇文章主要介紹了JS實(shí)現(xiàn)掃碼槍掃描二維碼功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

掃碼槍掃描二維碼,具體內(nèi)容如下所示:

業(yè)務(wù)需求要將數(shù)據(jù)生成二維碼,并用掃碼槍掃出數(shù)據(jù)上傳到服務(wù)端。

先上代碼吧,之后再完善注意點(diǎn)

this.start = new Date().getTime()
  let code = ''
  let lastTime, nextTime
  let lastCode, nextCode
  let that = this
  window.document.onkeypress = function (e) {
   if (window.event) { // IE
    nextCode = e.keyCode
   } else if (e.which) { // Netscape/Firefox/Opera
    nextCode = e.which
   }
   console.time()
   console.log('nextCode', nextCode)
   if (e.which === 13) {
    if (code.length < 3) return // 手動(dòng)輸入的時(shí)間不會(huì)讓code的長(zhǎng)度大于2,所以這里只會(huì)對(duì)掃碼槍有
    console.log(code)
    console.log('掃碼結(jié)束')
    console.timeEnd()
    that.parseQRCode(code) // 獲取到掃碼槍輸入的內(nèi)容,做別的操作
    code = ''
    lastCode = ''
    lastTime = ''
    return
   }
   nextTime = new Date().getTime()
   if (!lastTime && !lastCode) {
    console.log('掃碼開(kāi)始。。。')
    code += e.key
   }
   if (lastCode && lastTime && nextTime - lastTime > 500) { // 當(dāng)掃碼前有keypress事件時(shí),防止首字缺失
    console.log('防止首字缺失。。。')
    code = e.key
   } else if (lastCode && lastTime) {
    console.log('掃碼中。。。')
    code += e.key
   }
   lastCode = nextCode
   lastTime = nextTime
  }

總結(jié)

以上所述是小編給大家介紹的JS實(shí)現(xiàn)掃碼槍掃描二維碼功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論