From ab2d85a0d82e7a3f3a4dcc767c36ae77b920f43e Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Tue, 10 Jul 2018 18:47:01 -0400 Subject: [PATCH] Change from setInterval checking to using setTimeout this is much more sane --- client.js | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/client.js b/client.js index b33c6ee..60ef6a6 100644 --- a/client.js +++ b/client.js @@ -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);