1

I've got a working PhoneGap app that I'm trying to add a QR scanner to. To do this, I'm using PhoneGap Build's BarcodeScanner plugin. The issue that I'm having is that upon a scan completing, an alert will cause the app to freeze.

The relevant JavaScript is

var options=""
options += '<p>'+formData["form"][formPart][1]+'</p>'
options += '<a data-role="button" data-rel="dialog" formPart="'+formPart+'"id="Cap-'+formPart+'">Capture Image</a>'
options += '<p id="Cap-Data"></p>'
$('#formContent').append(options);
$('#Cap-'+formPart).on("tap",function(event){
var scanner = cordova.require("cordova/plugin/BarcodeScanner");
scanner.scan(
 function (result) {
  var FP = $(this).attr("formPart");
  $('#Cap-Data').html(result.text);
   alert(result.text);
  }, 
  function (error) {
   alert("Scanning failed: " + error);
  }
 );
});

Any help on this would be much appreciated.

magmastonealex
  • 426
  • 4
  • 16

1 Answers1

4

The problem is functions like alert or prompt stop the execution completely until they return. Try putting the alert code in setTimeout() No timeout needed though, you can set it to 0 ms. so it will immediately occur but won't block the flow.

setTimeout(function() {
  alert(result.text);
}, 0);

This question might be a good read about why setTimeout(fn, 0) helps in these situations.

Community
  • 1
  • 1
keune
  • 5,779
  • 4
  • 34
  • 50