我在UIWebView HTML页面中使用JavaScript Web worker技术来实现Titanium应用程序中的后台任务。它在iOS上工作正常但在Android上没有运气,我甚至无法弄清楚错误细节。这是脚本代码:
<script>
function startWorker(){
var worker = new Worker('/worker.js');
// message event handler
worker.onmessage = function (event) {
// logging
Ti.API.error( 'WebWorkerMessage:'+ JSON.stringify(event) );
// update the html page - not actually needed, just for demostration should the WebView be visible
document.getElementById('result').textContent = event.data;
// fire a titanium app event
Ti.App.fireEvent( "WORKER", { data: event.data } );
};
// error event handler
worker.onerror = function(event){
Ti.API.error( 'WebWorkerError:'+ JSON.stringify(event) );
};
}
</script>
一旦从我的Alloy控制器评估此脚本,就会调用错误处理程序。我正在Android 4.4.2设备上测试这个脚本,这个错误的日志是没用的:
WebWorkerError:{"cancelBubble":false,"returnValue":true,"srcElement":{},"defaultPrevented":false,"timeStamp":1422484158088,"cancelable":true,"bubbles":false,"eventPhase":2,"currentTarget":{},"target":{},"type":"error"}
有什么建议吗?