2017-04-21 61 views
0

我們有一個應用程序可與科爾多瓦[PhoneGap的] 我們希望執行結果的離線保存到高速緩存內存如何在PhoneGap的從阿賈克斯存儲到localStorage的

結果來自服務器[PHP] 比使用ajax &在web視圖中顯示的javascript結果

當用戶處於離線模式[無線/ 3G]時,我嘗試使用localStorage保留上次使用的值[2]。使用jQuery USD/EURO或USD/ILS即

正常工作

 $('.loading').fadeOut(); 

     var $response   = $(result); 
     var result    = $response.filter('#result').html(); 
     var result_1   = $response.filter('#result_1').html(); 
     var result_10   = $response.filter('#result_10').html(); 
     var result_100   = $response.filter('#result_100').html(); 
     var result_1000   = $response.filter('#result_1000').html(); 

     $('.convertfrom_txt').each(function() { $(this).text(cfrom_str); }) 
     $('.convertto_txt').each(function() { $(this).text(cto_str); }) 

     $('#cresult').text(result + ' ' + cto_str); 
     $('#one_unit').text(result_1); 
     $('#ten_unit').text(result_10); 
     $('#hundred_unit').text(result_100); 
     $('#thousand_unit').text(result_1000); 

離線假設值的日常變化。 [貨幣]

var currency = [10]; //10 currencies max to save offline 

var currency = result_1 ; //save result of 1 coin to array 

var offline; 
//Check connection 
function onDeviceReady() { 
    var offlineData = window.localStorage.getItem("result_1"); 

    if (window.navigator.onLine) offline = false; 
} 

if (offline == false) 
    { 
     document.getElementById('result_1').value = "currency[0]"; 
     document.getElementById('result_10').value = "currency[0]*10"; 
     document.getElementById('result_100').value = "currency[0]*100"; 
     document.getElementById('result_1000').value = "currency[0]*1000"; 
     document.getElementById('time').value = "time"; 
    } 

result_1表示一次轉換的值。 我們希望保存的1美元,並在一個陣列1歐元,離線

我不敢肯定值這更好的方式來選擇

localStorage的拍攝形式的官方 Docs

var storage = localStorage; 

var value = storage.getItem(key); // Pass a key name to get its value. 

storage.setItem(key, value) // Pass a key name and its value to add or update that key. 

storage.removeItem(key) // Pass a key name to remove that key from storage. 
  • 此類本地存儲是否可以在普通Android設備下工作?
  • 感謝

    +0

    查看更多信息[這裏](https://cordova.apache.org/docs/en/latest/cordova/storage/storage.html) –

    +0

    是什麼你的問題?如何[在本地存儲陣列](http://stackoverflow.com/questions/3357553/how-do-i-store-an-array-in-localstorage)或什麼? –

    +0

    是的Pavlo你實際上把一個鏈接,我把問題 – eyalix

    回答

    1

    LocalStorage被所有科爾多瓦平臺,包括Android支持。來自docs的信息。

    如何存儲陣列中的localStorage描述here

    +0

    我使用數字(3.5556)我需要一個數組,或者我應該使用它作爲string.thanks for參考 – eyalix

    +0

    據我所知,你可以使用JSON.parse(localStorage.getItem(yourStorageKey))來糾正來自'LocalStorage'的解析數字 –