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.

4

1 回答 1

4

问题是函数喜欢alertprompt完全停止执行,直到它们返回。尝试将警报代码设置为setTimeout()不需要超时,您可以将其设置为 0 毫秒。所以它会立即发生,但不会阻塞流动。

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

这个问题可能是关于为什么setTimeout(fn, 0)在这些情况下有帮助的好读物。

于 2013-07-29T16:10:31.997 回答