index.html 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <body>
  4. <h1>Data push using REST demo</h1>
  5. <div id="UI" style="background: #fff; border:1px solid black; height: 10em; padding: 0.5em; overflow:auto;"></div>
  6. <div style="margin-top: 1em;">Event log:</div>
  7. <div id="log" style="background: #eee; height: 10em; padding: 0.5em; overflow:auto;"></div>
  8. </body>
  9. <script>
  10. var ui = document.getElementById('UI');
  11. var log = document.getElementById('log');
  12. var version = 0;
  13. const refreshUI = d => ui.innerHTML = JSON.stringify(d, null, 2)
  14. const watch = function() {
  15. var tid;
  16. fetch('/api/data', {method: 'POST', body:JSON.stringify({version})})
  17. .then(r => r.json())
  18. .then(r => {
  19. if (r.status) {
  20. log.innerHTML += 'Status: ' + r.status + '<br/>';
  21. } else {
  22. version = r.version;
  23. log.innerHTML += 'Version: ' + version + '<br/>';
  24. refreshUI(r.data);
  25. }
  26. })
  27. .catch(err => console.log(err));
  28. clearTimeout(tid);
  29. tid = setTimeout(watch, 1000);
  30. };
  31. watch();
  32. </script>
  33. </html>