From 31d059972c0b6d4e6f63bb6e0310d66ee5eaffcb Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Sat, 1 Sep 2018 12:56:18 -0700 Subject: [PATCH] fine tuning ajax timing, naming, and start script --- public/client.js | 62 ++++++++++++++++++++++++++----------------- src/routes/checkin.js | 2 +- start.sh | 2 +- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/public/client.js b/public/client.js index 3c9f658..21dc01c 100644 --- a/public/client.js +++ b/public/client.js @@ -6,38 +6,46 @@ $(function(){ }, 20000); $("#rfid").keydown(function(event){ - // cancel enter keys + // prevent enter key from submitting form; we want AJAX if possible if (event.keyCode === 13){ return false; } - + }).keyup(function(event){ // store value value = $("#rfid").val(); - // start processing after the first few chars - if (value.length >= 5) { + // start processing after the enter key + if (event.keyCode === 13) { // wait to send the request for a sec to let them finish typing setTimeout(function(){ // clear input box $("#rfid").val(null); // post value to server loading(true); - $.post("/checkin", {"rfid":value}, function(data){ - console.log(data); - if (data.hasOwnProperty("success")) { - if (data.success) { - success(data.name); + if (!window.checkinLoading) { + // use global var to prevent simultaneous posts + window.checkinLoading = true; + console.log("Checking in..."); + $.post("/checkin", {"rfid":value}, function(data){ + console.log(data); + if (data.hasOwnProperty("success")) { + if (data.success) { + success(data.name); + } else { + failure(); + } } else { - failure(); + error(); } - } else { + }, "json").fail(function(){ error(); - } - }, "json").fail(function(){ - error(); - }).always(function(){ - loading(false); - }); + }).always(function(){ + loading(false); + window.checkinLoading = false; + }); + } else { + console.log("Already checking in; canceled."); + } }, 250); } }); @@ -61,19 +69,25 @@ function reset(){ function success(name){ reset(); - $("#name").text(name); - $("#success").removeClass("dn"); - setTimeout(function(){ reset(); }, 5000); + setTimeout(function(){ + $("#name").text(name); + $("#success").removeClass("dn"); + setTimeout(function(){ reset(); }, 6000); // 6000 to match door.js(const DELAY) + }, 250); } function failure(){ reset(); - $("#failure").removeClass("dn"); - setTimeout(function(){ reset(); }, 5000); + setTimeout(function(){ + $("#failure").removeClass("dn"); + setTimeout(function(){ reset(); }, 6000); // 6000 to match door.js(const DELAY) + }, 250); } function error(){ reset(); - $("#error").removeClass("dn"); - setTimeout(function(){ reset(); }, 5000); + setTimeout(function(){ + $("#error").removeClass("dn"); + setTimeout(function(){ reset(); }, 6000); // 6000 to match door.js(const DELAY) + }, 250); } \ No newline at end of file diff --git a/src/routes/checkin.js b/src/routes/checkin.js index 31bcd20..800a0dc 100644 --- a/src/routes/checkin.js +++ b/src/routes/checkin.js @@ -26,7 +26,7 @@ module.exports = (req, res) => { function reponse(req,res,path,success,name=null) { console.log(req.get('accept')); if(/application\/json/.test(req.get('accept'))) { - res.json({"success":success, "path":path}) + res.json({"success":success, "path":path, "name":name}) } else { res.redirect(path) } diff --git a/start.sh b/start.sh index f1ac784..2d7b88f 100755 --- a/start.sh +++ b/start.sh @@ -2,4 +2,4 @@ ~/.nvm/versions/node/v10.6.0/bin/forever start --workingDir /home/pi/doorlock /home/pi/doorlock/src/server.js echo "Loading Browser..." sleep 5 -chromium-browser --kiosk localhost:3000 +chromium-browser --incognito --kiosk --app=http://localhost:3000