const scanner={ /** * 监听document的扫描枪活动,扫描后调用doFunc执行,doFunc(str) * str是扫到的内容,在doFunc内执行 * @param doFunc */ monitorScan:function(doFunc){ let tightInputInterval=50; let leastCodeLength=3; let lastPressedAt=0; let scannedStr=""; $(document).on('keyup',function(e){ let nowAtMilli=(new Date()).getTime(); let inputInterval=nowAtMilli-lastPressedAt; if (inputInterval=leastCodeLength){ doFunc(scannedStr); } scannedStr=''; } }else{ if(e.key!=='Enter'){ scannedStr=e.key; }else{ scannedStr=''; } } lastPressedAt=nowAtMilli; }) }, monitorFocusingTarget:function(doFunc){ $('input').on('focus',function(e){ doFunc(e.target); }) }, }; module.exports=scanner;