0

I have a counter script on a website on the Internet and it (shows online on the server in gta) how can I make the counter work without reloading the page?

counter script

  var ip = "rage2.grand-rp.su:22005";
  $.getJSON('https://cdn.rage.mp/master', function(masterlist) {
      $.each(masterlist, function(key, result) {
          if(key == ip) {
              document.getElementById('online').innerHTML = result.players;
              return false;              
          }
      });
  });
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

1 Answers1

0

You could wrap the code in an function, then use 'setInterval' to run the function as often as you like.

<script>
    // How often to run the update function in ms (1000ms = 1sec)
    var updateInterval = 1000;

    setInterval(updatePlayerCount, updateInterval);

    // Function to run update logic
    function updatePlayerCount() {
        var ip = "rage2.grand-rp.su:22005";

        $.getJSON('https://cdn.rage.mp/master', function(masterlist) {
            $.each(masterlist, function(key, result) {
                if(key == ip) {
                    document.getElementById('online').innerHTML = result.players;
                    return false;              
                }
            });
        });
    }
</script>
ninja
  • 2,233
  • 1
  • 15
  • 15