Download the data and save it in the indexeddb database. Periodically, I have a database crash and lose access to it. Please give me a solution on how to use indexeddb asynchronously!
Example code that I use now:
var dataTotal = 0; var threads = 6; //openIndexeddbConnection(); function start(total){ dataTotal = total; for (var i = 0; i < threads; i++) { loadData(i); } } function loadData(dataNum){ var dataNext = dataNum + threads; if(dataNext > dataTotal){ //checkEnd(); return; } $.ajax({ url: baseUrl, data: {offset: dataNum}, success: function (data) { successData(dataNext, data); }, type: 'GET' }); } function successData(dataNext, data){ var dataArray = data.split(';'); saveData(dataArray); loadData(dataNext); } function saveData(dataArray){ putItem(); function putItem(i) { var count = i || 0; if(dataArray.length <= i){ return; } var transaction = Indexeddb.transaction([dataTableName], "readwrite"); transaction.onsuccess = function (event) { //continue putItem(count); }; var objectStore = transaction.objectStore(dataTableName); var request = objectStore.add({data: dataArray[count++]}); } }
source share