2017-10-22 18:00:42 +00:00
|
|
|
/* Baby Buddy Timer
|
2017-08-20 15:23:32 +00:00
|
|
|
*
|
|
|
|
* Uses a supplied ID to run a timer. The element using the ID must have
|
|
|
|
* three children with the following classes:
|
|
|
|
* * timer-seconds
|
|
|
|
* * timer-minutes
|
|
|
|
* * timer-hours
|
|
|
|
*/
|
2017-10-22 18:00:42 +00:00
|
|
|
BabyBuddy.Timer = function ($) {
|
2017-08-20 15:09:40 +00:00
|
|
|
var runIntervalId = null;
|
2017-10-28 02:28:38 +00:00
|
|
|
var timerId = null;
|
2017-08-20 15:09:40 +00:00
|
|
|
var timerElement = null;
|
2022-08-14 17:37:02 +00:00
|
|
|
var lastUpdate = new Date();
|
2019-08-23 03:35:52 +00:00
|
|
|
var hidden = null;
|
2017-08-20 15:09:40 +00:00
|
|
|
|
2017-08-20 15:23:32 +00:00
|
|
|
var Timer = {
|
2017-10-28 02:28:38 +00:00
|
|
|
run: function(timer_id, element_id) {
|
|
|
|
timerId = timer_id;
|
|
|
|
timerElement = $('#' + element_id);
|
2017-08-20 15:09:40 +00:00
|
|
|
|
2022-08-14 17:37:02 +00:00
|
|
|
if (timerElement.length === 0) {
|
2017-08-20 15:23:32 +00:00
|
|
|
console.error('BBTimer: Timer element not found.');
|
2017-08-20 15:09:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-14 17:37:02 +00:00
|
|
|
if (timerElement.find('.timer-seconds').length === 0
|
|
|
|
|| timerElement.find('.timer-minutes').length === 0
|
|
|
|
|| timerElement.find('.timer-hours').length === 0) {
|
2017-08-20 15:09:40 +00:00
|
|
|
console.error('BBTimer: Element does not contain expected children.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
runIntervalId = setInterval(this.tick, 1000);
|
2017-10-28 02:28:38 +00:00
|
|
|
|
|
|
|
// If the page just came in to view, update the timer data with the
|
|
|
|
// current actual duration. This will (potentially) help mobile
|
|
|
|
// phones that lock with the timer page open.
|
2019-08-23 03:35:52 +00:00
|
|
|
if (typeof document.hidden !== "undefined") {
|
|
|
|
hidden = "hidden";
|
|
|
|
}
|
|
|
|
else if (typeof document.msHidden !== "undefined") {
|
|
|
|
hidden = "msHidden";
|
|
|
|
}
|
|
|
|
else if (typeof document.webkitHidden !== "undefined") {
|
|
|
|
hidden = "webkitHidden";
|
|
|
|
}
|
|
|
|
window.addEventListener('focus', Timer.handleVisibilityChange, false);
|
|
|
|
},
|
|
|
|
|
|
|
|
handleVisibilityChange: function() {
|
2022-08-14 17:37:02 +00:00
|
|
|
if (!document[hidden] && (new Date()) - lastUpdate > 1) {
|
2019-08-23 03:35:52 +00:00
|
|
|
Timer.update();
|
|
|
|
}
|
2017-08-20 15:09:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
tick: function() {
|
|
|
|
var s = timerElement.find('.timer-seconds');
|
|
|
|
var seconds = Number(s.text());
|
|
|
|
if (seconds < 59) {
|
|
|
|
s.text(seconds + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s.text(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
var m = timerElement.find('.timer-minutes');
|
|
|
|
var minutes = Number(m.text());
|
|
|
|
if (minutes < 59) {
|
|
|
|
m.text(minutes + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m.text(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
var h = timerElement.find('.timer-hours');
|
|
|
|
var hours = Number(h.text());
|
|
|
|
h.text(hours + 1);
|
2017-10-28 02:28:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
update: function() {
|
|
|
|
$.get('/api/timers/' + timerId + '/', function(data) {
|
|
|
|
if (data && 'duration' in data) {
|
2017-10-29 19:47:26 +00:00
|
|
|
clearInterval(runIntervalId);
|
2022-08-14 17:37:02 +00:00
|
|
|
var duration = data.duration.split(/[\s:.]/)
|
|
|
|
if (duration.length === 5) {
|
|
|
|
duration[0] = parseInt(duration[0]) * 24 + parseInt(duration[1]);
|
|
|
|
duration[1] = duration[2];
|
|
|
|
duration[2] = duration[3];
|
|
|
|
}
|
|
|
|
timerElement.find('.timer-hours').text(parseInt(duration[0]));
|
|
|
|
timerElement.find('.timer-minutes').text(parseInt(duration[1]));
|
|
|
|
timerElement.find('.timer-seconds').text(parseInt(duration[2]));
|
|
|
|
lastUpdate = new Date()
|
2022-08-14 20:55:57 +00:00
|
|
|
runIntervalId = setInterval(Timer.tick, 1000);
|
2017-10-28 02:28:38 +00:00
|
|
|
}
|
|
|
|
});
|
2017-08-20 15:09:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-20 15:23:32 +00:00
|
|
|
return Timer;
|
2017-08-20 15:09:40 +00:00
|
|
|
}(jQuery);
|