非常感谢您的建议 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),但是当我重新打开数据库时,不需要升级。