PhoneGap条形码扫描仪导致iOS应用程序挂起

PhoneGap BarcodeScanner causing iOS app to hang

本文关键字:iOS 应用程序 挂起 条形码 扫描仪 PhoneGap      更新时间:2023-09-26

我有一个正在运行的PhoneGap应用程序,我正在尝试添加一个QR扫描仪。为此,我使用了PhoneGapBuild的BarcodeScanner插件。我遇到的问题是,扫描完成后,警报会导致应用程序冻结。

相关的JavaScript是

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

如有任何帮助,我们将不胜感激。

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

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

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