JavaScript Battery API

The Battery API has been updated; read JavaScript Battery API Update to see the new code pattern!

Battery API已更新; 阅读JavaScript Battery API Update ,查看新的代码模式!

Mozilla Aurora 11 was recently released with a bevy of new features. One of those great new features is their initial implementation of the Battery Status API. This simple API provides you information about the battery's current charge level, its charging status, and allows you to be notified of changes via a few events. Let's check it out!

Mozilla Aurora 11最近发布, 具有许多新功能 。 这些出色的新功能之一是它们对Battery Status API的最初实现。 这个简单的API为您提供有关电池当前电量,其充电状态的信息,并允许您通过一些事件来通知更改。 让我们来看看!

The battery object is located at window.navigator.battery, but since this is Mozilla's initial offering and the API is not yet cemented, you'll need to use window.navigator.mozBattery. The helpful mozBattery properties include:

电池对象位于window.navigator.battery ,但是由于这是Mozilla的初始产品并且API尚未确定,因此您需要使用window.navigator.mozBattery 。 有用的mozBattery属性包括:

  • charging: Represents if the system's battery is charging. The attribute must be set to false if the battery is discharging, and set to true, if the battery is charging, full, the implementation is unable to report the state, or there is no battery attached to the system, or otherwise.

    charging:表示系统电池是否正在充电。 如果电池正在放电,则必须将该属性设置为false;如果电池正在充电,已充满,该实现无法报告状态或没有电池连接至系统,则必须将该属性设置为true。

  • chargingTime: Represents the time remaining in seconds until the system's battery is fully charged.

    chargingTime:表示系统电池充满电之前剩余的时间(以秒为单位)。

  • dischargingTime: Represents the time remaining in seconds until the system's battery is completely discharged and the system is about to be suspended.

    dischargingTime:时间dischargingTime:表示系统电池完全放电且系统即将挂起之前的剩余时间(以秒为单位)。

  • level: Represents the current battery level scaled from 0 to 1.0. The attribute must be set to 0 if the system's battery is depleted and the system is about to be suspended, and to 1.0 if the battery is full, the implementation is unable to report the battery's level, or there is no battery attached to the system.

    level:表示当前电池电量,从0到1.0缩放。 如果系统的电池已用完并且系统即将挂起,则必须将该属性设置为0,如果电池已满,则该属性必须设置为1.0,实现无法报告电池的电量,或者系统未连接电池。

Events for each of these statuses are provided, including onchargingchange, onchargingtimechange, ondischargingtimechange, and onlevelchange. Basic usage is simple:

提供了每种状态的事件,包括onchargingchangeonchargingtimechangeondischargingtimechangeonlevelchange 。 基本用法很简单:


// Get the battery!
var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery;

// A few useful battery properties
console.warn("Battery charging: ", battery.charging); // true
console.warn("Battery level: ", battery.level); // 0.58
console.warn("Battery discharging time: ", battery.dischargingTime);

// Add a few event listeners
battery.addEventListener("chargingchange", function(e) {
	console.warn("Battery charge change: ", battery.charging);
}, false);
battery.addEventListener("chargingtimechange", function(e) {
	console.warn("Battery charge time change: ", battery.chargingTime);
}, false);
battery.addEventListener("dischargingtimechange", function(e) {
	console.warn("Battery discharging time change: ", battery.dischargingTime);
}, false);
battery.addEventListener("levelchange", function(e) {
	console.warn("Battery level change: ", battery.level);
}, false);


I promised a simple API, didn't I? The battery API is the perfect API: simple, effective, and focused!

我答应过一个简单的API,不是吗? 电池API是完美的API:简单,有效和专注!

So why use the battery API? Since many mobile apps live inside a browser wrapper (aren't completely "native"), it's great to have access to system information. Some processes are power-intensive and it may bear warning the user before starting the process that their device is low on battery. Either way, this simple API's true usefulness should become apparent soon!

那么为什么要使用电池API? 由于许多移动应用程序都位于浏览器包装内(并非完全“本机”),因此能够访问系统信息非常好。 某些过程耗电,可能会在开始该过程之前警告用户其设备的电池电量低。 无论哪种方式,这个简单的API的真正用途都将很快变得显而易见!

翻译自: https://davidwalsh.name/battery-api

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值