I'm working on barcode scanner with my online POS system. My problem now is as follows:
var barcode = 0;
// handle a key value being entered by either keyboard or scanner
$("#scanInput").keypress(function (e) {
// restart the timer
if (timing) {
clearTimeout(timing);
}
// handle the key event
if (e.which == 13) {
// Enter key was entered
// don't submit the form
e.preventDefault();
// has the user finished entering manually?
if ($("#scanInput").val().length >= minChars){
userFinishedEntering = true; // incase the user pressed the enter key
/* alert("class "+$(this).attr("class"));
alert("ID "+$(this).attr("id"));
alert("value "+$(this).val());
/*get the class starts*/
var data;
var id = $(this).val();
$.ajax({
type: "GET",
dataType: "html",
url: "fetch_class.php?id="+id,
data: data,
success: function(data) {
//alert("scanclass"+data);
barcode = data;
alert("inside "+barcode);//correct value
}
});
alert("outside "+barcode);
//says 0 as I declared it so in the above
//also this(alert("outside "+barcode);)
executed first prior to the ajax functionalert("inside "+barcode);
, but why?
All I need is the same value of the barcode inside the ajax success function to be available outside of it too, so that I may pass that value to other functions. How do I make the barcode global,please?
.
/*get the class ends*/
inputComplete(barcode);
}
}
else {
// some other key value was entered
// could be the last character
inputStop = performance.now();
lastKey = e.which;
// don't assume it's finished just yet
userFinishedEntering = false;
// is this the first character?
if (!inputStart) {
firstKey = e.which;
inputStart = inputStop;
// watch for a loss of focus
$("body").on("blur", "#scanInput", inputBlur);
}
// start the timer again
timing = setTimeout(inputTimeoutHandler, 500);
}
});