gpt4 book ai didi

javascript - Phonegap - onDeviceReady() 未被触发

转载 作者:行者123 更新时间:2023-11-30 07:41:46 24 4
gpt4 key购买 nike

请看看并帮我解决这个问题。我头痛了2天。永远不会调用 onDeviceReady 函数。这是我的代码:

    <!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>DemoSimpleControls</title>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<link rel="shortcut icon" href="images/favicon.png">
<link rel="apple-touch-icon" href="images/apple-touch-icon.png">
<link href="jqueryMobile/jquery.mobile-1.3.0.css" rel="stylesheet">
<link rel="stylesheet" href="css/DemoSimpleControls.css">

<script src="jqueryMobile/jquery.mobile-1.3.0.js"></script>
<script src="../js/jquery-1.9.1.min.js"></script>
< script type="text/javascript" charset="utf-8" src="../cordova-2.5.0.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Handler for .ready() called.
document.addEventListener("deviceready", onDeviceReady, true);
});
$(function() {
document.addEventListener("deviceready", onDeviceReady, true);
});
function onDeviceReady(){
alert("ready");
$("#mysavedData").html("XYZ");
$("#mysavedData").html(window.localStorage.getItem("data"));
}

</script>
</head>

<body id="content" onLoad="onLoad();" >
<div data-role="page" id="page">
<div data-role="header" >
<a data-rel="back" href="#" >Back</a>
<h1>My page</h1>

</div>
<div data-role="content" style="padding: 15px" data-theme="e">
<div id="mysavedData">My data</div>

</div>

</div>
</body>
</html>

最佳答案

那个事件很糟糕,有很多问题,不值得使用它。相反,只需通过检查 window.device 进行轮询,直到 PhoneGap 准备就绪。

演示: jsFiddle

Javascript:

function initializePhoneGap( success, failure ) {
var timer = window.setInterval( function () {
if ( window.device ) {
window.clearInterval( timer );
success();
};
}, 100 );
window.setTimeout( function () { //failsafe
if ( !window.device ) { //phonegap failed
window.clearInterval( timer );
failure();
};
}, 5000 ); //5 seconds
};

window.onload = function () {
initializePhoneGap( function success() {
//start app
document.getElementById( 'result' ).textContent = 'phonegap: success';
}, function failure() {
//phonegap failed
document.getElementById( 'result' ).textContent = 'phonegap: failure';
} );

};

HTML:

should fail on desktop after 5 seconds
<div id="result">phonegap:</div>

关于javascript - Phonegap - onDeviceReady() 未被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15606870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com