frontend/js/common/util/timer_time_remaining.js
Paulus Schoutsen 783f356679
Add timer card and badge (#810)
* Add timer card and badge

* Disable interval on disconnect

* Tests!

* One more test case

* Remove padStart

* Remove state from timer state card
2018-01-19 09:26:06 -08:00

14 lines
417 B
JavaScript

import durationToSeconds from './duration_to_seconds.js';
export default function timerTimeRemaining(stateObj) {
let timeRemaining = durationToSeconds(stateObj.attributes.remaining);
if (stateObj.state === 'active') {
const now = new Date();
const madeActive = new Date(stateObj.last_changed);
timeRemaining = Math.max(timeRemaining - ((now - madeActive) / 1000), 0);
}
return timeRemaining;
}