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: 
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.
I've made a snippet to demonstrate how this can be done. Each function sets the callback to an existing function rather than creating a new one to set it to. I got it working with setInterval so you can see what's happening better; you can see that each new scan scan overwrites the next box, proving that the focus wraps around.

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

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