polling demo

This commit is contained in:
Ryan Peters 2023-01-09 15:54:20 -05:00
parent 2caa82b8f4
commit 4673220624
2 changed files with 24 additions and 0 deletions

View File

@ -14,6 +14,10 @@ public class ApiController : ControllerBase
this.noteService = noteService; this.noteService = noteService;
} }
[HttpGet]
[Route("get")]
public string Get() => noteService.Get();
[HttpPost] [HttpPost]
[Route("save")] [Route("save")]
public async Task<bool> Save() public async Task<bool> Save()

View File

@ -20,6 +20,18 @@
}); });
}; };
var poller = null;
let pollJob = function () {
poller = setInterval(function () {
$.get('/api/get', function (content) {
$('textarea').val(content);
});
}, 30000);
}
$(function () { $(function () {
// set dark mode // set dark mode
@ -28,18 +40,26 @@ $(function () {
} }
var timer = null; var timer = null;
let ignoredKeyCodes = [17, 18, 20, 27, 37, 38, 39, 40, 91]; let ignoredKeyCodes = [17, 18, 20, 27, 37, 38, 39, 40, 91];
// start polling
pollJob();
// save after half-second delay after typing // save after half-second delay after typing
$('textarea').keyup(function (e) { $('textarea').keyup(function (e) {
clearTimeout(timer); clearTimeout(timer);
clearInterval(poller);
// only set timer if key isn't ignored // only set timer if key isn't ignored
if (!ignoredKeyCodes.includes(e.keyCode)) { if (!ignoredKeyCodes.includes(e.keyCode)) {
timer = setTimeout(function () { timer = setTimeout(function () {
saveContent(); saveContent();
}, 1000); }, 1000);
pollJob();
} }
}); });