Guest User

simple json file sync

a guest
May 9th, 2022
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.36 KB | None | 0 0
  1.  
  2. <!DOCTYPE html>
  3. <html>
  4.  
  5. <head>
  6.   <!-- this changed -->
  7.   <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
  8.   <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
  9. </head>
  10.  
  11. <body>
  12.  
  13. <p id="demo"></p>
  14.  
  15. <script>
  16. function myDisplayer(some) {
  17.   document.getElementById("demo").innerHTML = some;
  18. }
  19.  
  20. function getFile(myCallback) {
  21.   let req = new XMLHttpRequest();
  22.   req.open('GET', "test.json");
  23.   req.onload = function() {
  24.     if (req.status === 200) {
  25.       myCallback(this.responseText);
  26.     } else {
  27.       myCallback("Error: " + req.status);
  28.     }
  29.   };
  30.  
  31.  
  32.   req.send();
  33. }
  34.  
  35. getFile(myDisplayer);
  36. </script>
  37.  
  38.  
  39. <script type="text/javascript">
  40.     document.getElementById('demo')
  41.         .addEventListener('change', function () {
  42.  
  43.            let fileReader = new FileReader();
  44.             fileReader.onload = function () {
  45.                let parsedJSON = Object.values(JSON.parse(fileReader.result));
  46.                console.log(parsedJSON);
  47.                
  48.  
  49.                
  50.                
  51.                 // your code to consume the json
  52.  
  53.             }
  54.             fileReader.readAsText(this.files[0]);
  55.         });
  56.        
  57. </script>
  58.  
  59.  
  60.  
  61. <script>
  62.  
  63.  
  64. // setInterval(function() {
  65. //
  66. //  $(demo).load('test.json');
  67. //}, 1000);
  68.  
  69.  
  70. </script>
  71.  
  72.  
  73.  
  74.  
  75. </body>
  76.  
  77. </html>
Advertisement
Add Comment
Please, Sign In to add comment