Question: Issue : I am using Cognex MX-1000 to scan barcodes. I have three textboxes and I am trying to scan barcodes and change the focus to next textbox. But after I scan the third textbox the value is always entered in third textbox even when the focus is on first textbox. 

Cause : My code is not able to exit the innermost Setresultcallback function. Below is my method does anyone know how to exit the innermost callback function?

```
 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;
    });

```

Answer: 
That's because you leave the callback for the third field in the last callback. The following should fix the problem.

```
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;
    });

```
The problem is that you don't rebind to your first callback. Since you always do the same things into your callbacks :

1. Get the value
2. Write the value to the current input
3. Focus the next input

It can be simplified to something like this :

```js
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);
```

```html
<input type="text" id="TextRec1" />
<input type="text" id="TextRec2" />
<input type="text" id="TextRec3" />
```

Don't mind `var me = { ...` I wanted something to simulate the behavior you're describing.