亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

掃碼槍掃描二維碼,具體內(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 // 手動輸入的時間不會讓code的長度大于2,所以這里只會對掃碼槍有
    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('掃碼開始。。。')
    code += e.key
   }
   if (lastCode && lastTime && nextTime - lastTime > 500) { // 當(dāng)掃碼前有keypress事件時,防止首字缺失
    console.log('防止首字缺失。。。')
    code = e.key
   } else if (lastCode && lastTime) {
    console.log('掃碼中。。。')
    code += e.key
   }
   lastCode = nextCode
   lastTime = nextTime
  }

總結(jié)

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

相關(guān)文章

最新評論