Change from setInterval checking to using setTimeout

this is much more sane
This commit is contained in:
Adam Goldsmith 2018-07-10 18:47:01 -04:00
parent 0f9a202b6f
commit ab2d85a0d8

View File

@ -2,9 +2,7 @@ let calls = {};
let messages = [];
const checkTime = 1000; // 1 second
const timeOut = 20 * 60 * 1000; // 20 Minutes
//const timeOut = 60 * 1000; // 1 Minute
const timeoutLength = 20 * 60 * 1000; // 20 Minutes
Notification.requestPermission(permission => {
if (permission === "granted") {
@ -12,32 +10,33 @@ Notification.requestPermission(permission => {
}
});
function checkNotHeard() {
let now = new Date();
for (let call in calls) {
let lastHeard = new Date(calls[call]);
console.log(call, now - lastHeard);
if (now - lastHeard > timeOut) {
new Notification(`${call} has not been heard for 20 Minutes!`,
{body: `Last Heard: ${lastHeard}`});
}
};
function alertNotHeard(callsign) {
new Notification(`${callsign} has not been heard for 20 Minutes!`,
{body: `Last Heard: ${calls[callsign].lastHeard.toLocaleTimeString('en-GB')}`});
}
let aprsStream = new WebSocket("ws://localhost:1234");
aprsStream.onmessage = function(event) {
let message = JSON.parse(event.data);
let call = `${message.from.call}-${message.from.ssid || 0}`;
let date = message.recieved;
let callsign = `${message.from.call}-${message.from.ssid || 0}`;
let date = new Date(message.recieved);
console.log(message);
if (call in calls) {
message.delta = date - calls[call];
}
calls[call] = date;
messages.push(message);
if (!(callsign in calls)) {
calls[callsign] = {
lastHeard: date,
};
}
else {
window.clearTimeout(calls[callsign].timeout);
}
calls[callsign].delta = date - calls[callsign].lastHeard;
calls[callsign].timeout = window.setTimeout(
alertNotHeard, timeoutLength, callsign);
};
window.setInterval(checkNotHeard, checkTime);