我需要用javascript创建某种基本的pos。我有一个像键盘一样工作的条形码扫描仪。所以我想自动检测输入来自条形码,然后创建一个包含该代码的报告。我认为一个好主意是计算打字速度的差异因为扫描仪非常快:
if there a dalay between keyup > 300ms = another scan
var speed = new Date().getTime() - lastkey;
lastkey = (new Date()).getTime();
console.log(speed);
if(speed > 300) {
//little delay for prevent computer overhead o_O
var create_d = setTimeout(createchange,300)
} else {
//barcode sanning continue
clearTimeout(create_d)
}
答案 0 :(得分:1)
使用纯JS:
document.body.onkeydown = function ()
{
var time = this._time;
var timestamp = new Date().getTime();
if (time)
console.log(timestamp - time);
this._time = timestamp;
}
控制台将显示keydown事件之间的差异(以毫秒为单位)