2015-06-10 77 views

回答

2

您可以使用LocalStorage爲:

document.addEventListener('deviceready', function() 
{ 
    if (typeof window.localStorage.getItem('firstTime') === 'undefined') 
    { 
     // Execute some code for the installation 
     // ... 

     window.localStorage.setItem('firstTime', 0); 
    } 
}); 
0

同時添加事件偵聽器,有效運行一個單一的時間,在Ivan's answer,是正確的,語法需要略有不同。

當運行下面的代碼:

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

在JavaScript控制檯中看到以下內容:

First time? 
null 
object 
false 

此外,對於Storage.getItem()Mozilla的文檔指出returns 'null' when a non-existent key is requested

返回

包含密鑰值的DOMString。如果該鍵不存在,則返回null。

因此,爲了使這項工作,我需要使用下面的代碼:

document.addEventListener('deviceready', function() 
{ 
    if (window.localStorage.getItem('firstTime') == null) 
    { 
     // Execute some code for the installation 
     // ... 

     window.localStorage.setItem('firstTime', 0); 
    } 
}); 
相關問題