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?