navigator.getbattery找不到

发布于 2025-01-21 14:07:43 字数 1491 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不一样的天空 2025-01-28 14:07:43

在Google Chrome(版本100)上运行时,您遇到的错误不会出现。

看起来一些浏览器不支持navigator.getBattery()。请参阅

浏览器版本
chromech(> 38)
边缘✅(> 79)
firefox❌(43-52)
IE❌(无支持)
operaopera✅(> 25)
safari❌(无支持

) ,❌=不支持。

如果您注意到不支持所使用的浏览器,则应升级或切换它,运行代码,然后可以使用。


另外,代码中的另一个问题是,当不存在电池时,您正在尝试获取电池。这可能会出现错误。

看来您想使用返回的BatteryManager对象,在这种情况下,a。因此,要修复它,只需使用下面的代码即可。

const m = a.level * 100 + "%";

现在,这是从已经定义的a变量中获取Level属性。将代码更改为此,它应该正确工作。

The error that you are getting is not coming up when running on Google Chrome (version 100).

It looks like a few browsers don't support navigator.getBattery(). See the browser compatibility on MDN, or see the table below.

BrowserVersions
Chrome✅ (>38)
Edge✅ (>79)
Firefox❌ (43-52)
IE❌ (no support)
Opera✅ (>25)
Safari❌ (no support)

Key: ✅ = Supported, ❌ = Not supported.

If you notice that the browser that you are using isn't supported, you should upgrade or switch it, run the code, and it should work then.


Also, another problem in your code is that you are trying to get battery.level, when battery doesn't exist. This will likely throw an error.

It looks like you want to use the returned BatteryManager object, in this case, a. So, to fix it, simply use the code below.

const m = a.level * 100 + "%";

This is now getting the level property from the already-defined a variable. When you change your code to this, it should work correctly.

千柳 2025-01-28 14:07:43

您遇到了使用变量a的问题,但正在引用电池。查看我的编辑和工作演示:

function batttick() {
  navigator.getBattery().then(battery => {
    let m = ""
    let c = ""
    m = battery.level * 100 + "%"

    if (battery.charging) {
      m += " ⚡";
      c = "green";
    }
    console.log(c, m);
    document.getElementById("batt").innerHTML = m;
    document.getElementById("batt").style.color = c;
  })
}
batttick()
<div id="batt"></div>

You had an issue where you were using a variable a but were referencing battery. See my edits and working demo:

function batttick() {
  navigator.getBattery().then(battery => {
    let m = ""
    let c = ""
    m = battery.level * 100 + "%"

    if (battery.charging) {
      m += " ⚡";
      c = "green";
    }
    console.log(c, m);
    document.getElementById("batt").innerHTML = m;
    document.getElementById("batt").style.color = c;
  })
}
batttick()
<div id="batt"></div>

梅倚清风 2025-01-28 14:07:43

此特定错误消息的一个常见原因是,如果页面未在安全上下文(HTTPS)中运行,则许多浏览器会阻止电池API。

检查兼容性图表的“安全上下文必需的上下文”行: https://developer.mozilla.org/en-us/docs/web/api/navigator/getBattery#browser_compatibility

由于这是此错误消息的最佳Google搜索结果,即使值得一提的是,即使这个问题被标记为解决。

One common cause of this specific error message is that many browsers block the battery API if the page is not running in a secure context (HTTPS).

Check the "Secure Context Required" row of the compatibility chart: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getBattery#browser_compatibility

Since this is now the top google search result for this error message, it's worth noting, even though this question is marked as solved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文