2

I am pretty lazy, I've made a code that is fetching data from the database and then reloading a page on the index every second if something is changing, so it's visible without needing to refresh the page.

What alternative is faster?

        function getLowPlayers() { 
        var httpd; 
        if (window.XMLHttpRequest) { 
            httpd = new XMLHttpRequest(); 
        } else if (window.ActiveXObject) { 
            httpd = new ActiveXObject("Microsoft.XMLHTTP"); 
        } 

        httpd.onreadystatechange=function() 
        { 
            if (httpd.readyState==4 && httpd.status==200) 
            { 
                document.getElementById("lowplayers").innerHTML= httpd.responseText;  
                setTimeout(getLowPlayers, 1000);  
            } 
        }   

        httpd.open("GET", "includes/ajax.php?noCache=" + Math.random(), true); 
        httpd.send(); 
    } 

or this one:

function ajaxCall() {
    $.ajax({
        url: "ajax.php", 
        success: (function (result) {
             var res = $.parseJSON(result);
             var str1 = "<center><img src='https://steamcommunity-a.akamaihd.net/economy/image/";
            var str3 = " ' height='60' width='70'/></br>";
            var mes = [];
            var div = [];   
        })
    })
};

I know that it is a silly solution to do like this, I could setup a socket.io server, but I think it's too much of work.

I understand that with many visitors, the ajax.php file would send way to many queries per second, is that healthy for the database? Or is it fine with todays internet speed and hosting services?

Which one is faster? Or do you guys have any better solution?

Jax Flaxx
  • 57
  • 5

1 Answers1

0

The two codes are more or less the same, the first is written in javascript vainlla, and the second is in JQuery and therefore is shorter and simpler.

If I had to decide, I would choose the second code since it is easier to read and maintain.

Regarding the performance between those 2 codes is practically the same, although if there were many connections the option to use ajax would not be very correct since each server request occupies "X" memory that if we are talking about large numbers would mean that the page was will load slowly because of the waiting queues.

So the best idea as you say is to confiure socket.io and try to update the page with calls from the server to customers.

eborrallo
  • 750
  • 1
  • 8
  • 17