Page 1 of 1

Better way to refresh div?

Posted: Tue Mar 11, 2014 6:31 pm
by Klown
Hi Guys!

I continue to have trouble with some basic stuff. I have a browser game which updates each players stats, usually very simple stats such as: Rank: Recruite, Score: 10, Gold: 10, etc..... usually consists of a div being refreshed with a php page that has 1 database query to get updated info every 20 seconds. Here is the problem, as I gain more players on my game, it's starting to lag more and more.

I'm a complete noob with JQuery, Ajax, Javascript, etc.. So I have been reading about Node.js / Socket.io and was wondering if these would be a solution for me to prevent lag for my game and players. It's becoming a problem which will drive players away from the game, which I pay quite a lot in advertising to gain.

This game is a hobby for me and does not provide any positive income, so any help from anyone would be greatly appreciated. I am hoping someone has a place I can simply learn how to refresh a divs content with a php page which Echo's 1 line of text for the stats in my game.

I have a lot of stats to update constantly and currently use code such as this:

Code: Select all

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
      var auto_refresh = setInterval(
      function()
      {
         $('#showStats').load('stats_update_query.php');

      }, 20000);

</script>

the above code lags because there are other .load calls being used - i need a solution which can handle this sort of thing with less lag for the end user.

Any advice and help would be very greatly appreciated! If needed I would be willing to work with someone on a small budget to help implement a working solution.

At the moment my game has anywhere from 5-15 players online at a single time and is growing daily.

Thanks in advance,

Dustin Bowers

Re: Better way to refresh div?

Posted: Tue Mar 11, 2014 6:38 pm
by Xaos
Does it need to be dynamically refreshed? Can it not just be refreshed using PHP only and a page refresh?

Re: Better way to refresh div?

Posted: Tue Mar 11, 2014 7:15 pm
by Mardonis
What is the site address?

Re: Better way to refresh div?

Posted: Tue Mar 11, 2014 8:47 pm
by Klown
I don't see why it would need to be dynamically refreshed. Do you think doing something like nesting an iframe in the div and setting its refresh would work better than what i am currently doing?

Here is a question, because I'm a noob and don't know the answer.

Does the client or server process the request for the refresh - the way I am currently doing it with JQuery, and if I were to do nested iframes with a Meta Refresh would that put more stress on the client side and lag players who have slower internet speeds?

Re: Better way to refresh div?

Posted: Tue Mar 11, 2014 10:46 pm
by KyleMassacre
I think that regardless of how you wish to do it will put some sort of strain either client or server side if you want it to be dynamic. I'm not the biggest fan of iframes at all so I would personally not use them.

Loading player stats automatically to me is a waste of performing something like that to idle players. Most of the stats wouldn't/shouldn't really change all that often without the players doing something right? If a player didn't buy something is their money going to change without them knowing? So for that kind of stuff I would stick to just do it on a click of a link or button then you are saving resources for people who need it or are using it.

Also, I too, am a n00b when it comes to a js lib and don't know if this would change much(someone else can probably give a better response to this) but you can maybe try to json encode/decode instead of just using .load(). Another alternative would possibly be to serve them the least common or needed stats on a higher interval?

Re: Better way to refresh div?

Posted: Wed Mar 12, 2014 12:52 am
by Xaos
If you're using PHP just straight up echo it it'll change in page refresh and won't be dynamic which will cut lag

Re: Better way to refresh div?

Posted: Wed Mar 12, 2014 5:59 am
by MikuzA
Hello,
My suggestion would be to integrate the status-update refresh to other actions in the game, and perhaps add a fancy refresh-button to initiate the status update.

jQuery wait intervals sounds kinda nasty to me.

Re: Better way to refresh div?

Posted: Fri Mar 14, 2014 7:40 pm
by MikuzA
Yeah, but iframe is kinda 90's :D but yeah, simplest perhaps!

Re: Better way to refresh div?

Posted: Sun Mar 16, 2014 5:37 pm
by hallsofvallhalla
I think you need to start with the design question then go from there. Why would you need to refresh every 20 seconds?

If it is a must then you need to only refresh the stats so use ajax, it is very simple

without jquery

put this in your index file in a javascript tag and have a timeout function call it.

Code: Select all

function GetStats(){
	var ajaxRequest; 
	try
	{	
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			///this is where you change the divs!
			document.getElementById("StatsDiv").innerHTML = ajaxRequest.responseText;
		}
	}
	var PlayerId = '<?php echo $PlayerId; ?>';
	var queryString = "?PlayerId=" + PlayerId;
	ajaxRequest.open("GET", "ajaxFunction/GetStats.php" + queryString, true);
	ajaxRequest.send(null); 
}
Then call your query in the GetStats.php file

get the Playerid by

Code: Select all

$PlayerId = $_GET['PlayerId'];
in the same file whatever you echo get returned to ajaxRequest.responseText, so echo "Strength = " . $playerinfo3['Strength'] . "<br>Wisdom = " ect...

Re: Better way to refresh div?

Posted: Mon Mar 17, 2014 6:44 am
by Winawer
What does stats_update_query.php do? 15 players polling at 20 second intervals shouldn't lag at all unless you're doing some heavy stuff.