javascript – 使用indexedDB时,获取“setVersion”不是函数错误

前端之家收集整理的这篇文章主要介绍了javascript – 使用indexedDB时,获取“setVersion”不是函数错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我尝试使用以下代码来测试IndexedDB的性能.
代码http://www.html5rocks.com/en/tutorials/indexeddb/todo/修改,
它在chrome中运行良好,但在Firefox 10中失败,说“db.setVersion不是函数”.
我想知道如何修改代码以使其在Firefox中运行?

        var count=0;
        var MAX=10;
        var times=3;
        var allTime;
        var stime;
        var etime;

        var html5rocks = {};
        var indexedDB = window.indexedDB || window.webkitIndexedDB ||
                        window.mozIndexedDB;

        if ('webkitIndexedDB' in window) {
            window.IDBTransaction = window.webkitIDBTransaction;
            window.IDBKeyRange = window.webkitIDBKeyRange;
        }

        html5rocks.indexedDB = {};
        html5rocks.indexedDB.db = null;

        html5rocks.indexedDB.onerror = function(e) {
            //console.log(e);
            alert("Why didn't you allow my web app to use IndexedDB?!");  
        };

        html5rocks.indexedDB.open = function(type) {

        var request = indexedDB.open("todos");
            request.onsuccess = function(e) {
            var v = "1.20";
            html5rocks.indexedDB.db = e.target.result;
            var db = html5rocks.indexedDB.db;
            // We can only create Object stores in a setVersion transaction;
            if (v!= db.version) {

                var setVrequest = db.setVersion(v);
                // onsuccess is the only place we can create Object Stores
                setVrequest.onerror = html5rocks.indexedDB.onerror;
                setVrequest.onsuccess = function(e) {
                    if(db.objectStoreNames.contains("todo")) {
                        db.deleteObjectStore("todo");
                    }

                    var store = db.createObjectStore("todo",{keyPath: "number"});
                        addTest();
                };

            }
            else addTest();
            };

            request.onerror = html5rocks.indexedDB.onerror;

        }

        html5rocks.indexedDB.addTodo = function(todoText,num) {
            var db = html5rocks.indexedDB.db;
            var trans = db.transaction(["todo"],IDBTransaction.READ_WRITE);
            var store = trans.objectStore("todo");

            var data = {
            "text": todoText,"number": num
            };

            var request = store.put(data);

            request.onsuccess = function(e) {
                count++;
                if(count>=times*MAX)
                {
                    etime=new Date;
                    var t=document.getElementById('result').innerHTML;
                    document.getElementById('result').innerHTML=t+((etime.valueOf()-stime.valueOf())/times)+"
最佳答案
该规范尚未最终确定.目前,这是作为Gecko中的属性mozIndexedDB和Chrome中的webkitIndexedDB提供的,直到标准最终确定.所以你必须为moz写作.现在这段代码仅适用于webkit.

https://developer.mozilla.org/en/IndexedDB

猜你在找的JavaScript相关文章