当前位置: 首页 > 工具软件 > localForage > 使用案例 >

本地离线存储localforage

卢开济
2023-12-01

关于
HTMl5的sessionStorage和localStorage–灵活使用传送门

localforage 用法类似转载自

API
1)setItem(key, value, successCallback):创建一个键,参数是键名、键值、回调函数,回调函数的参数是对应的键值。

// Unlike localStorage, you can store non-strings.
localforage.setItem('my array', [1, 2, 'three']).then(function(value) {
    // This will output `1`.
    console.log(value[0]);
}).catch(function(err) {
    // This code runs if there were any errors
    console.log(err);
});
2)getItem(key, successCallback):获取数据并使用回调函数

localforage.getItem('somekey', function(err, value) {
    // Run this code once the value has been
    // loaded from the offline store.
    console.log(value);
});
3)removeItem(key, successCallback):移除对应的键

localforage.removeItem('somekey').then(function() {
    // Run this code once the key has been removed.
    console.log('Key is cleared!');
}).catch(function(err) {
    // This code runs if there were any errors
    console.log(err);
});
4)clear(successCallback):清除所有键。

localforage.clear().then(function() {
    // Run this code once the database has been entirely deleted.
    console.log('Database is now empty.');
}).catch(function(err) {
    // This code runs if there were any errors
    console.log(err);
});
5)length(successCallback):获得离线存储的总的键数。

localforage.length().then(function(numberOfKeys) {
    // Outputs the length of the database.
    console.log(numberOfKeys);
}).catch(function(err) {
    // This code runs if there were any errors
    console.log(err);
});
6)key(keyIndex, successCallback):根据键索引得到键值。
7)keys(successCallback):得到所有的键索引。
8)iterate(iteratorCallback, successCallback):对数据库中的每一个键值对调用回调函数,回调函数的参数分别是键值、键索引、迭代次数(基于1)。

// The same code, but using ES6 Promises.
localforage.iterate(function(value, key, iterationNumber) {
    // Resulting key/value pair -- this callback
    // will be executed for every item in the
    // database.
    console.log([key, value]);
}).then(function() {
    console.log('Iteration has completed');
}).catch(function(err) {
    // This code runs if there were any errors
    console.log(err);
});
或许并不一定要迭代所有的键值对,此时只要返回一个非undefined类型值,就可以提前结束迭代,这个返回值会作为successCallback的参数。这个方法有点问题,返回的值不太对:

<!DOCTYPE html>
<html>
  <head>
    <title>Listing 2.1</title>
    <script type="text/javascript" src="https://raw.githubusercontent.com/mozilla/localForage/master/dist/localforage.min.js">       </script>

    <script type="text/javascript">
      localforage.setItem('array', [1, 2,'three']);
      localforage.setItem('string', 'people');
      localforage.setItem('number1', 5);
      localforage.setItem('number2', 6);
      localforage.iterate(function(value, key, iterationNumber) {
         if (iterationNumber < 2) {
                console.log([key, value]);
            } else {
                return [key, value];
            }
      }).then(function(result) {
          console.log('Iteration has completed at '+ result);
      }).catch(function(err) {
          console.log(err);
      });
    </script>
  </head>
  <body>
  </body>
</html>
前面提到,localForage在默认情况下会优先采用使用IDB、WebSQL、localStorage进行后台存储,但是可以通过setDriver()进行后台设置,如果设置的机制在当前浏览器并不支持,则还是按照默认选择。
setDriver(driverName)/setDriver([driverName, nextDriverName]):设置后台支持机制,参数是localforage.INDEXEDDB、localforage.WEBSQL、localforage.LOCALSTORAGE。

github使用前需要下载js 库引用

 类似资料: