topic_plus_load.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. function topic_plus_load(name, sub, num) {
  2. var test = setInterval(function() {
  3. var url = "/api/topic/" + name + "/sub/" + sub + "?num=" + num + "&render=1";
  4. var p_data = document.getElementById("plus_topic");
  5. var n_data = '';
  6. var n_num = 1;
  7. var xhr = new XMLHttpRequest();
  8. xhr.open("GET", url, true);
  9. xhr.send(null);
  10. xhr.onreadystatechange = function() {
  11. if(this.readyState === 4 && this.status === 200 && this.responseText && this.responseText !== {}) {
  12. t_data = JSON.parse(this.responseText);
  13. for(key in t_data) {
  14. n_data += t_data[key]['data'];
  15. n_num = key;
  16. }
  17. p_data.innerHTML += n_data;
  18. // https://programmingsummaries.tistory.com/379
  19. var options = {
  20. body: 'New ' + n_num + ' topic'
  21. }
  22. var notification = new Notification("openNAMU", options);
  23. setTimeout(function () {
  24. notification.close();
  25. }, 5000);
  26. topic_plus_load(name, sub, String(Number(num) + 1));
  27. clearInterval(test);
  28. }
  29. }
  30. }, 2000);
  31. }