非常感谢您的建议Kristof,我更正了我的代码:
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;
var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
const dbName = "mediasDB";
const db_store = "medias";
var db;
function openDB(dbName, dbVersion) {
var request = indexedDB.open(dbName, dbVersion);
request.onupgradeneeded = function (evt) {
console.log("Upgrade");
console.log('Creating objectStore');
var store = evt.currentTarget.result.createObjectStore(db_store);
store.transaction.oncomplete = function (evt) {
storeMedias(mediaData, function (media) {
var store = getObjectStore(db_store, "readwrite")
console.log(media);
store.put(media, media.url);
console.log("Rajout d'un media dans la BD");
});
};
};
request.onsuccess = function (event) {
console.log("openDB DONE");
db = this.result;
};
request.onerror = function (event) {
console.log(event.target.result);
};
}
openDB(dbName, 1);
//On déclenche le défilement des médias au bout de 5 secondes
setInterval(function () {
$.getJSON("remoteFileJson", function (data) {
if (data.revision != db.version) {
console.log('Mise à jour de la BD');
openDB(dbName, data.revision);
} else {
console.log('La base de données est à jour');
}
});
}, 10000);
每隔十秒钟,我会得到一个JSON,它提供了我的修订版本号(它大于我检查的db.version),但是当我重新打开OpenDB时,不需要调用upgrade。
最佳答案
您不能手动创建版本更改事务。唯一的方法是在打开indexeddb连接时更改版本号。有关更多信息:http://www.kristofdegrave.be/2012/01/indexed-db-defining-your-structure-new.html
var dbRequest = indexeddb.open("name", 3);
dbRequest.onupgradeneeded = function (e)
{
var versionChangeTrans = e.target.transaction;
}