Avoiding Timeouts on iPhone
I am looking for a good strategy to insert about 2500 id firstName lastName tuples (600KB) from an AJAX call. The timeout happens after the AJAX call completes and while I am storing the data in the localstorage database. The data is in XML format. What I am doing now is converting the XML data into an object by doing a jQuery.each() over the data with this,
associateNames[index] = {
firstName:jQuery(value).find("First").text(),
lastName:jQuery(value).find("Last").text(),
id:jQuery(value).find("ID").text()
};
Then I pass this object to a function that cre开发者_如何转开发ates a table and inserts this data. The main part of that function is.
query2.executeSql('CREATE TABLE directory (id unique, fullName)', [],
// On Success
function(query, result) {
jQuery.each(data, function(index, value) {
var querySQL = 'INSERT INTO directory (id, fullName) VALUES (' + value.id +
', "'+ encodeURI(value.firstName) + encodeURI(value.lastName) + '")';
query.executeSql(querySQL);
});
},
The iPhone doesn't support web workers. There should be a way to use setInterval() and process pieces of data at a time, but I am getting stuck. Please let me know if you have an idea.
Thanks!
Am I right in thinking the "data" is an array? If so you could do something like this:
...
//on Success
function(query, result){
setTimeout(function(){ //just in case Ajax took a while too..
var executeBatch = function(startIndex){
for(var i = startIndex; i < startIndex+50; i++){
if(i >= data.length) return;
//do your execute query on data[i]..
}
//after short while do the next batch..
setTimeout(function(){ executeBatch(startIndex+50) }, 50);
};
executeBatch(0);
}, 100);
}
);
精彩评论