test.html 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:wb="http://open.weibo.com/wb" style="overflow: hidden;">
  3. <head>
  4. <script>
  5. function poll() {
  6. var start = 0;
  7. var time = null;
  8. var xhr = new XMLHttpRequest();
  9. var data = "";
  10. xhr.open("POST", "http://127.0.0.1:1473", true);
  11. xhr.onreadystatechange = function () {
  12. if (xhr.readyState == 3 && xhr.status == 200) {
  13. clearTimeout(time);
  14. var len = xhr.responseText.length;
  15. data += xhr.responseText.substring(start, len);
  16. start = len;
  17. console.log(data)
  18. time = setTimeout(function () {
  19. clearTimeout(time);
  20. data = "";
  21. }, 100);
  22. }
  23. }
  24. xhr.onerror = function (err) {
  25. console.log(err);
  26. }
  27. xhr.send();
  28. }
  29. window.onload = function () {
  30. poll()
  31. }
  32. </script>
  33. </head>
  34. <body>
  35. </body>
  36. </html>