Better way to refresh div?

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
Klown
Posts: 90
Joined: Tue Feb 22, 2011 5:59 am

Better way to refresh div?

Post 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
if( $myGames != "Crap" ) {
  • please_donate( $money );
} else {
  • just_enjoy();
}
User avatar
Xaos
Posts: 946
Joined: Wed Jan 11, 2012 4:01 am

Re: Better way to refresh div?

Post by Xaos »

Does it need to be dynamically refreshed? Can it not just be refreshed using PHP only and a page refresh?
Mardonis
Posts: 139
Joined: Wed Jun 29, 2011 7:54 pm

Re: Better way to refresh div?

Post by Mardonis »

What is the site address?
Klown
Posts: 90
Joined: Tue Feb 22, 2011 5:59 am

Re: Better way to refresh div?

Post 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?
if( $myGames != "Crap" ) {
  • please_donate( $money );
} else {
  • just_enjoy();
}
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: Better way to refresh div?

Post 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?
User avatar
Xaos
Posts: 946
Joined: Wed Jan 11, 2012 4:01 am

Re: Better way to refresh div?

Post 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
User avatar
MikuzA
Posts: 395
Joined: Thu Aug 08, 2013 8:57 am

Re: Better way to refresh div?

Post 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.
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
MikuzA
Posts: 395
Joined: Thu Aug 08, 2013 8:57 am

Re: Better way to refresh div?

Post by MikuzA »

Yeah, but iframe is kinda 90's :D but yeah, simplest perhaps!
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Better way to refresh div?

Post 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...
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Better way to refresh div?

Post 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.
Post Reply

Return to “Advanced Help and Support”