How do we keep refreshing a certain part of the page with JQ
How do we keep refreshing a certain part of the page with JQ
So I have an excluded file called PlayerInfo.php
This file later on gets included in every page in my game. It shows how much does the player have HP or MP or Gold left.
Everytime the player buys something from the store, sometimes the 'gold bar' still shows the old gold.. I mean if you had 30 gold and you spent 15, it still shows 30 unless it gets refreshed first..
Same thing happens with the HP and MP bar when on the battle system..
So I want to refresh this part of div dynamically per x ms with jQuery.
I'm thinking of defining a function on JS first. and then put SetInterval in the end.
Can anyone show me the right way without screwing down the page?
This file later on gets included in every page in my game. It shows how much does the player have HP or MP or Gold left.
Everytime the player buys something from the store, sometimes the 'gold bar' still shows the old gold.. I mean if you had 30 gold and you spent 15, it still shows 30 unless it gets refreshed first..
Same thing happens with the HP and MP bar when on the battle system..
So I want to refresh this part of div dynamically per x ms with jQuery.
I'm thinking of defining a function on JS first. and then put SetInterval in the end.
Can anyone show me the right way without screwing down the page?
Skype: mrvictordiaz
- a_bertrand
- Posts: 1536
- Joined: Mon Feb 25, 2013 1:46 pm
Re: How do we keep refreshing a certain part of the page wit
You can certainly use ajax to load back data from the server and update divs or others dynamically. However keep in mind, that this will use some resources. So avoid to do too many Ajax call
Creator of Dot World Maker
Mad programmer and annoying composer
Mad programmer and annoying composer
Re: How do we keep refreshing a certain part of the page wit
ok, but why not? I thought those are one effective ways?
Skype: mrvictordiaz
- a_bertrand
- Posts: 1536
- Joined: Mon Feb 25, 2013 1:46 pm
Re: How do we keep refreshing a certain part of the page wit
If your update is based on calculation, you could simply send while the page load the number of sec it needs to wait till completion and only then reload to show the new effect. This is just an example BTW.
Creator of Dot World Maker
Mad programmer and annoying composer
Mad programmer and annoying composer
Re: How do we keep refreshing a certain part of the page wit
Why not update the gold when the player buys stuff? Your example doesn't really require polling for anything.
Re: How do we keep refreshing a certain part of the page wit
I already did this. I even put the update query on top of everything, but for some reason in PHP it just can't (although in some part I found it working without I even know why lol)Winawer wrote:Why not update the gold when the player buys stuff? Your example doesn't really require polling for anything.
So a_bertrand, since I have just gotten into jQuery and JScript, I might have to greet the codes more to get familiar with. So I have some question also about jQuery in general. Frankly, AJAX. Also could you suggest me which one should I use best? The jQuery post or simply the AJAX itself?
If I have this
Code: Select all
$.ajax({
});
And, for example in url.php I echo some variable. Will it be able to be shown in the initial page? Mainly it's because I just moved from PHP and I think the core mechanic is far different in signing form.
Oh FYI, I think I already found one good code to do it, but they just 'do' what I want, without I understand them. And most sources don't provide enough info about what does each line of code do. So it would be good if I get to know how it works
THanks!
Skype: mrvictordiaz
- a_bertrand
- Posts: 1536
- Joined: Mon Feb 25, 2013 1:46 pm
Re: How do we keep refreshing a certain part of the page wit
Check out how to implement ajax in PHP, there is TONS of examples and tutorials. It's not overall complex, mostly it's like loading a normal page (for the server it is actually exactly that), however instead of displaying that page, you use the result differently.
Creator of Dot World Maker
Mad programmer and annoying composer
Mad programmer and annoying composer
Re: How do we keep refreshing a certain part of the page wit
Of course it can, submit form->update data->show data is pretty much the most common thing that's done in PHP. Your code is just wrong.Gunner wrote:I already did this. I even put the update query on top of everything, but for some reason in PHP it just can't (although in some part I found it working without I even know why lol)Winawer wrote:Why not update the gold when the player buys stuff? Your example doesn't really require polling for anything.
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: How do we keep refreshing a certain part of the page wit
As has already been mentioned, PHP will work just fine for this. You don't need to alter the display of the gold unless the player does something. If the page reloads, you can just change it in the PHP and then redisplay the correct amount on the page load. If you do it through AJAX, you would just return the new gold amount directly to the Javascript on the client-side and use Javascript to update the amount in the browser.Gunner wrote:I already did this. I even put the update query on top of everything, but for some reason in PHP it just can't (although in some part I found it working without I even know why lol)
There are few differences. $.post() is just a shortcut function that sets up some default values to the $.ajax() function.Gunner wrote:Also could you suggest me which one should I use best? The jQuery post or simply the AJAX itself?
In most cases you are fine to use $.post() or $.get(), but $.ajax() does have some extra options that aren't usable in the shortcut functions, such as events for each stage and result of the AJAX call. For this reason you will find some developers always use $.ajax, even when they don't need the extra functionality. But it is up to you. I extensively use the shortcut functions myself since they are just less typing and a bit more concise.
The indelible lord of tl;dr
Re: How do we keep refreshing a certain part of the page wit
You were right, PHP could make this really easily. I just didn't notice that I put the query in the middle of my body tag, whereas the sidebar containing player hp mp gold etc etc were in the left sidebar so they get executed first! Damn it worked like an epic charm *sigh*
Yay so the idea to do that with JQ is out. But I still need to stick with it as it seems really handy and I will just use the $.ajax
thanks for your time everyone appreciated
Yay so the idea to do that with JQ is out. But I still need to stick with it as it seems really handy and I will just use the $.ajax
thanks for your time everyone appreciated
Skype: mrvictordiaz