查找Storage后,我想尝试重置临时存储以进行测试。
window.webkitStorageInfo.requestQuota(webkitStorageInfo.TEMPORARY, 5*1024*1024,
function( bytes ) {
console.log( "Quota is available: " + bytes );
},
function( e ) {
console.log( "Error allocating quota: " + e );
});
window.webkitStorageInfo.queryUsageAndQuota(webkitStorageInfo.TEMPORARY,
//the type can be either TEMPORARY or PERSISTENT
function(used, remaining) {
console.log("Used quota: " + used + ", current quota: " + remaining);
}, function(e) {
console.log('Error', e);
});
requestQuota()设置临时存储大小,queryUsageAndQuota()告诉我当前的配额是多少。但是,尽管设置了新的配额,但在chrome浏览器中查看控制台日志时,它却没有反映在
queryUsageAndQuota()
中。我看起来像:Quota is available: 5242880
Used quota: 0, current quota: 214748364
即使我期望可用的数量和当前的配额相同。这是为什么?
最佳答案
比较这两个输出
代码1
window.webkitStorageInfo.requestQuota(webkitStorageInfo.TEMPORARY,
5188955171*1024*1024,
function( bytes ) {
console.log( "Quota is available: " + bytes );
},
function( e ) {
console.log( "Error allocating quota: " + e );
});
window.webkitStorageInfo.queryUsageAndQuota(webkitStorageInfo.TEMPORARY,
//the type can be either TEMPORARY or PERSISTENT
function(used, remaining) {
console.log("Used quota: " + used + ", current quota: " + remaining);
}, function(e) {
console.log('Error', e);
});
输出1
Quota is available: 5188982205
Used quota: 492, current quota: 5188982205
代码2
window.webkitStorageInfo.requestQuota(webkitStorageInfo.TEMPORARY, 5*1024*1024,
function( bytes ) {
console.log( "Quota is available: " + bytes );
},
function( e ) {
console.log( "Error allocating quota: " + e );
});
window.webkitStorageInfo.queryUsageAndQuota(webkitStorageInfo.TEMPORARY,
//the type can be either TEMPORARY or PERSISTENT
function(used, remaining) {
console.log("Used quota: " + used + ", current quota: " + remaining);
}, function(e) {
console.log('Error', e);
});
输出2
Quota is available: 5242880
Used quota: 0, current quota: 214748364
在您的情况下,您请求的是次要配额
5242880
,该配额已经可用并且少于可用数量,因此它返回一个successCallback
,其中请求了配额!通过请求大量来重置配额!window.webkitStorageInfo.requestQuota用于请求更多的可用存储空间,Chrome如何自动为您的应用提供临时存储,因此您不需要请求分配(最多共享池的20%)
参考文献
Storage