![]() ![]() Opera followed suit in version 25 (October 2014) and finally Chrome in version 40 (April 2015). FireFox mobile has had the longest support, ranging back to early days of version 18 (January 2013). Mobile browsers are where support is most important. On a positive note, a good number of browsers now support it (as of November 2015), so now's the perfect time to get up and running with this API. Since the Battery API is not finalized, it doesn't have universal support across all browsers yet. Overall, the idea is that we can use the Battery API to provide some good defaults that save battery power, while providing more options for interacting with the website. Providing an option or setting so users could set what they want is also a good idea. We could use the state to determine how frequently our tasks should be called, in order to minimize network usage. We could use the battery level to determine the default interval in which background tasks are run - such as Ajax and Web Workers.We could reduce complex CSS animations or interface elements and provide a more basic interface.An option to download the high quality stream could be provided. We could set video streams or playback to lower quality levels by default, making data transfers are quicker (and thus requiring less work of the device).We could have two options displayed to the user, one to request it normally and another with high accuracy. We could customize the Geolocation elements so that they have a lower accuracy and don't request usage of the inbuilt GPS by default.Let's look at some of the ways we could save some battery power: (No one likes a website or web app that demolishes your battery.) ![]() Websites offer a range of useful but expensive operations that could be adjusted so that your users spend less of their battery on your website. What Could We Use This Information for Anyway?Įven though battery optimization generally isn't in the forefront of a developer's mind, it's a factor that should be considered, especially with everything moving towards mobiles. With this information, we can start to change the user experience to help the user. In addition, we can sometimes get the device's approximate charge or discharge time (the time until the device is either charged or dead). Since we can access the user's battery state, we can determine their current battery level and whether the device is charging or discharging. What we can do, however, is provide additional functionality, or a more customized experience, if the current browser supports the API - i.e., using it in a progressive enhancement style. Navigator.getBattery() //returns a promiseīattery will be fully charged in minutes.Since the specification is not yet finalized, we can't count on developing websites or web apps that fully depend on the Battery API. Throw new Error('Battery API not supported on your device/computer') īatterySupported.innerHTML = ssage "Infinity" : parseInt(battery.dischargingTime / 60, 10) īattery.addEventListener("levelchange", setStatus, false) īattery.addEventListener("chargingchange", setStatus, false) īattery.addEventListener("chargingtimechange", setStatus, false) īattery.addEventListener("dischargingtimechange", setStatus, false) "Infinity" : parseInt(battery.chargingTime / 60, 10) īatteryDischarged.innerHTML = (battery.dischargingTime = "Infinity") ? Let batterySupported = document.getElementById("battery-supported"),īatteryLevel = document.getElementById("battery-level"),ĬhargingStatus = document.getElementById("charging-status"),īatter圜harged = document.getElementById("battery-charged"),īatteryDischarged = document.getElementById("battery-discharged") īatteryLevel.innerHTML = Math.round(battery.level * 100) + "%" ĬhargingStatus.innerHTML = (battery.charging) ? "" : "not " īatter圜harged.innerHTML = (battery.chargingTime = "Infinity") ? For example, I don't want the user to engage a heavy action that could probably lower his battery level to a critical point and make the critical action fail. The point of my algorithm when this snippet works is to define which action the user cannot perform regarding the remaining battery level. So what I understand is the object battery instantiated with the supposed battery level is returning nothing. Uncaught TypeError: Cannot read property 'addEventListener' of undefined The problem : Running Chrome 20+ version (currently 46), I still have an error on the console : So I tried the snippet of the Mozilla Foundation over in my JSFiddle. Here is the output I intend to get:īattery level : XXX% (depending the level) I want to get the battery level of the current system. !\ THE USE OF IS STRONGLY DISCOURAGED AND THIS ISSUE IS NOW NOT WORTH CHECKING THANK YOU /!\ ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |