From 4673220624222a377061693603ecb7e9d8897f29 Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Mon, 9 Jan 2023 15:54:20 -0500 Subject: [PATCH] polling demo --- Controllers/ApiController.cs | 4 ++++ wwwroot/js/site.js | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/Controllers/ApiController.cs b/Controllers/ApiController.cs index 4cbeeab..ad335fd 100644 --- a/Controllers/ApiController.cs +++ b/Controllers/ApiController.cs @@ -14,6 +14,10 @@ public class ApiController : ControllerBase this.noteService = noteService; } + [HttpGet] + [Route("get")] + public string Get() => noteService.Get(); + [HttpPost] [Route("save")] public async Task Save() diff --git a/wwwroot/js/site.js b/wwwroot/js/site.js index 347f204..86b699f 100644 --- a/wwwroot/js/site.js +++ b/wwwroot/js/site.js @@ -20,6 +20,18 @@ }); }; +var poller = null; + +let pollJob = function () { + + poller = setInterval(function () { + + $.get('/api/get', function (content) { + $('textarea').val(content); + }); + }, 30000); +} + $(function () { // set dark mode @@ -28,18 +40,26 @@ $(function () { } var timer = null; + let ignoredKeyCodes = [17, 18, 20, 27, 37, 38, 39, 40, 91]; + + // start polling + pollJob(); // save after half-second delay after typing $('textarea').keyup(function (e) { clearTimeout(timer); + clearInterval(poller); // only set timer if key isn't ignored if (!ignoredKeyCodes.includes(e.keyCode)) { + timer = setTimeout(function () { saveContent(); }, 1000); + + pollJob(); } });