我想在安装A Cordova / Phonegap 应用程序时调用一个函数(用于创建数据库并向其中插入记录)。
实际上我只想运行一次这个功能。
有什么建议吗?
答案 0 :(得分:3)
您可以使用LocalStorage:
document.addEventListener('deviceready', function()
{
if (typeof window.localStorage.getItem('firstTime') === 'undefined')
{
// Execute some code for the installation
// ...
window.localStorage.setItem('firstTime', 0);
}
});
答案 1 :(得分:0)
虽然添加一个事件监听器来有效地运行一次,就像在Ivan's answer中一样,但是语法需要略有不同。
运行以下代码时:
<script type="text/javascript">
jQuery(function($) {
$("#example-3" ).wizard({
transitions: {
q3: function( state, action ) {
var branch = state.step.find( "[name=q3]:selected" ).attr("class");
if (!branch ) {
????
}
return branch;
},
q5: function( state, action ) {
var branch = state.step.find( "[name=q5]:selected" ).attr("class");
if ( !branch ) {
//alert( "Please select a value to continue." );
}
return branch;
},
q6: function( state, action ) {
var branch = state.step.find( "[name=q6]:selected" ).attr("class");
if ( !branch ) {
//alert( "Please select a value to continue." );
}
return branch;
},
q7: function( state, action ) {
var branch = state.step.find( "[name=q7]:selected" ).attr("class");
if ( !branch ) {
//alert( "Please select a value to continue." );
}
return branch;
},
q8: function( state, action ) {
var branch = state.step.find( "[name=q8]:selected" ).attr("class");
if ( !branch ) {
//alert( "Please select a value to continue." );
}
return branch;
}
},
beforeForward: function( event, state ) {
return !!$( this ).wizard( "form" ).valid();
},
afterSelect: function( event, state ) {
//$( "#progressbar2" ).progressbar( "value", state.percentComplete );
$( "#location2" ).text(Math.round(state.percentComplete) + "%");
if($('#unqualified').is(':visible')) {
$(".pad").hide();
} else {
$(".pad").show();
};
}
}).validate({
rules :{
q1: {
required: true
},
q2: {
required: true
},
q3: {
required: true
},
q4: {
required: true
},
q5: {
required: true
},
q6: {
required: true
},
q7: {
required: true
},
q8: {
required: true
}
}
});
});
</script>
在Javascript控制台中可以看到以下内容:
console.log("First time?");
console.log(window.localStorage.getItem('firstTime'));
console.log(typeof window.localStorage.getItem('firstTime'));
console.log(typeof window.localStorage.getItem('firstTime') === 'undefined');
此外,Storage.getItem()的Mozilla文档说明returns 'null' when a non-existent key is requested:
<强>返回强>
包含键值的DOMString。如果密钥不存在,则返回null。
因此,为了使这项工作,我需要使用以下代码:
First time?
null
object
false