Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
estimate()
方法是StorageManager
的一个接口,用于估算某一个域名(或一个站点)下Storage Manager的总存储空间和已经使用了的存储空间。此方法为一个异步方法,如果此方法可用,那么其返回一个结果为resolved的Promise
对象。resolved接收的参数是一个带有已使用数据存储空间和总可用总存储空间的StorageEstimate
对象。
语法
var estimatePromise = StorageManager.estimate();
参数
无
返回值
StorageEstimate
类型的状态为resolved的Promise
此数据包含了此应用(或域名)可用的存储空间(StorageEstimate.quota
)和目前已经使用了的存储空间(StorageEstimate.usage
)。
这些值不是明确的数字,在进行压缩,重复数据删除和出于安全原因起见进行了混淆之后,这个数据是不精确的。
你可能会发现不同的应用或站点分配的存储空间不同,具体取决于用户访问频率,和网站受欢迎程度等数据。
示例
在这个示例中,我们使用estimate()得到目前所使用的存储空间占全部存储空间的百分比。
HTML 内容
<p>
You're currently using about <span id="percent">
</span>% of your available storage.
</p>
JavaScript 内容
navigator.storage.estimate().then(function(estimate) {
document.getElementById("percent").innerHTML =
(estimate.usage / estimate.quota * 100).toFixed(2);
});
结果
规范
Specification | Status | Comment |
---|---|---|
Storage estimate() |
Living Standard | Initial definition. |
浏览器兼容性
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
参见
- Storage API
Storage
, the object returned byWindow.localStorage
StorageManager
navigator.storage