我會做的是保存在localStorage數據(雖然此鏈接指向的PhoneGap文檔,localStorage的是HTML5功能,非常有用),並定期調用,將檢查是否有東西送功能。
因此,要檢查網絡連接的可用性:
function isOnline(){
try{
var status = navigator.network.connection.type;
var val = (status != 'none' && status != 'unknown');
return val;
}catch(err){ console.error('isOnline(): '+err); return false; }
}
包括以下幾行代碼添加一些功能,本地存儲,因爲它只是允許保存字符串,但是這兩個功能將能夠以存儲JSON數據:
Storage.prototype.setObject = function(key, value) {
this.setItem(key, JSON.stringify(value));
}
Storage.prototype.getObject = function(key) {
var value = this.getItem(key);
return value && JSON.parse(value);
}
在您的表單提交處理程序中,檢查是否存在連接。如果不是,保存要發送的數據:
if(isOnline()){
// do stuff
}else{
var data = // The data to be sent
var toBeSent = localStorage.getObject('toBeSent') || [];
toBeSent.push(data); // It's better to store the data in an array, in case there's more than a questioner to be sent
localStorage.setObject('toBeSent', toBeSent);
}
之後寫檢查和發送數據的功能:
function sendPending(){
var toBeSent = localStorage.getObject('toBeSent') || [];
var data;
for(var i in toBeSent){
data = toBeSent[i];
// send data
}
// Remove the list
localStorage.removeItem('toBeSent');
}
最後,執行定期的功能:
setInterval(sendPending, 60000); // The system will check every minute if there's something to send
您是否需要該應用程序來自動發送已經回答和存儲的提問者,或者需要用戶交互? – davids 2012-08-10 07:18:16
嗨大衛,感謝您的評論。如果自動更好..但不介意是否需要用戶交互(最多可同時按下一個或發送所有結果按鈕)。 – 2012-08-10 07:19:49