最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I get the devices battery level in javascript - Stack Overflow

programmeradmin4浏览0评论

I am doing a network call every 15 seconds in my app, and if the users device battery percent is lower than 20%, than I would like to do the call every 30 seconds instead. How do I get the user's devices current battery level? Is it possible? Any help would be appreciated.

I am doing a network call every 15 seconds in my app, and if the users device battery percent is lower than 20%, than I would like to do the call every 30 seconds instead. How do I get the user's devices current battery level? Is it possible? Any help would be appreciated.

Share Improve this question asked Jun 10, 2017 at 9:30 KingCoder11KingCoder11 4164 silver badges19 bronze badges 8
  • Man i don't know whether its done till now or not but I am inspired by the way you are going and considering things. Really appreciate. – geminiousgoel Commented Jun 10, 2017 at 9:35
  • I don't want to use up the users battery – KingCoder11 Commented Jun 10, 2017 at 9:35
  • up vote from my side. +1 and put it under my favorite. – geminiousgoel Commented Jun 10, 2017 at 9:36
  • Thanks, I am still searching on it – KingCoder11 Commented Jun 10, 2017 at 9:36
  • I think I found something here, but the battery api isnt supported on many modern browsers, like opera: sitepoint./html5-battery-status-api – KingCoder11 Commented Jun 10, 2017 at 9:40
 |  Show 3 more ments

1 Answer 1

Reset to default 7

Take a look at the Battery Status API. It doesn't work in all browsers, but it's a start.

For browsers that support it, something like this should work:

navigator.getBattery().then(function(battery) {
  battery.addEventListener('levelchange', function() {    
    // Do stuff when the level changes, you can get it
    // from battery.level
    document.write((battery.level*100)+"%");
  })
  document.write((battery.level*100)+"%");
});

发布评论

评论列表(0)

  1. 暂无评论