问题:我使用Cognex MX-1000扫描条形码。我有三个文本框,我正在尝试扫描条形码并将焦点更改为下一个文本框。但是在我扫描第三个文本框后,即使焦点位于第一个文本框,也始终在第三个文本框中输入值。
原因:我的代码无法退出最内层的Setresultcallback函数。以下是我的方法有谁知道如何退出最里面的回调函数?
me.scanner.setResultCallback(function (result) { //First callback
var barcode = result.readString;
var txt1 = barcode;
document.getElementById("TextBox1").value = txt1;
document.getElementById("TextBox2").focus();
me.scanner.setResultCallback(function (result) { //Second callback
var barcode = result.readString;
var txt2 = barcode;
document.getElementById("TextBox2").value = txt2;
document.getElementById("TextBox3").focus();
me.scanner.setResultCallback(function (result) { //Third Callback & stuck here!!! I want to exit this function
var barcode = result.readString;
var txt3 = barcode;
document.getElementById("TextBox3").value = txt3;
document.getElementById("TextBox1").focus();
return;
});
return;
});
return;
});
答案 0 :(得分:2)
那是因为你在最后一个回调中留下第三个字段的回调。以下内容应解决问题。
me.scanner.setResultCallback(/* Change here -> */ function firstFieldCallback(result) { //First callback
var barcode = result.readString;
var txt1 = barcode;
document.getElementById("TextRec1").value = txt1;
document.getElementById("TextRec2").focus();
me.scanner.setResultCallback(function (result) { //Second callback
var barcode = result.readString;
var txt2 = barcode;
document.getElementById("TextRec2").value = txt2;
document.getElementById("TextRec3").focus();
me.scanner.setResultCallback(function (result) { //Third Callback & stuck here!!! I want to exit this function
var barcode = result.readString;
var txt3 = barcode;
document.getElementById("TextRec3").value = txt3;
document.getElementById("TextRec1").focus();
/* Change here -> */ me.scanner.setResultCallback(firstFieldCallback);
return;
});
return;
});
return;
});
答案 1 :(得分:2)
问题在于您没有重新绑定到第一个回调。因为你总是在你的回调中做同样的事情:
可以将其简化为:
var me = {
scanner: {
setResultCallback: function(callback) {
setTimeout(function() {
callback({
readString: Math.floor(Math.random() * 100) + 1
})
}, 1000);
}
}
},
textboxes = ['TextRec1', 'TextRec2', 'TextRec3'],
index = 0;
function readValue(result) {
var barcode = result.readString;
document.getElementById(textboxes[index++]).value = barcode;
index = index === textboxes.length ? 0 : index;
document.getElementById(textboxes[index]).focus();
me.scanner.setResultCallback(readValue);
}
me.scanner.setResultCallback(readValue);
<input type="text" id="TextRec1" />
<input type="text" id="TextRec2" />
<input type="text" id="TextRec3" />
不介意var me = { ...
我想要模仿你所描述的行为。
答案 2 :(得分:1)
我已经制作了一个片段来演示如何做到这一点。每个函数都将回调设置为现有函数,而不是创建一个新函数来设置它。我让它与setInterval一起工作,这样你就可以看到更好的事情发生了什么。你可以看到每个新的扫描扫描都会覆盖下一个框,证明焦点在周围。
//Code to make this work in a snippet
var me = {};
me.scanner={};
me.scanner.setResultCallback = function(newcallback){scancallback=newcallback;};
//Set the callback to the first function; each function sets it to the next
me.scanner.setResultCallback(scan1);
//Simulate scanning stuff by manually calling the callback with time intervals; just pretend each time the function is called, it's called naturally by scanning with the scanner
setInterval(function(){
scancallback({randomField:"this is a scan result I guess",readString:Math.floor(Math.random()*10000)});
},1000);
function scan1(scanresult){
//Don't even need the variables, if you need them somehow you can add them back
document.getElementById("TextRec1").value =
scanresult.readString;
document.getElementById("TextRec2").focus();
me.scanner.setResultCallback(scan2);
}
function scan2(scanresult){
//Don't even need the variables, if you need them somehow you can add them back
document.getElementById("TextRec2").value =
scanresult.readString;
document.getElementById("TextRec3").focus();
me.scanner.setResultCallback(scan3);
}
function scan3(scanresult){
//Don't even need the variables, if you need them somehow you can add them back
document.getElementById("TextRec3").value =
scanresult.readString;
document.getElementById("TextRec1").focus();
me.scanner.setResultCallback(scan1);
}
&#13;
<input type="text" id="TextRec1"/>
<input type="text" id="TextRec2"/>
<input type="text" id="TextRec3"/>
&#13;