Welcome to WebmasterWorld Guest from 54.162.155.183

Forum Moderators: open

Message Too Old, No Replies

Ajax and javascript problem.

     
7:08 am on Sep 13, 2010 (gmt 0)

5+ Year Member



Hi all,

I'm trying to write a script that is kind of like a battle system, for an RPG Maker website.

For this script, ajax reloads periodically so that when 1 person does something on the page, the other can see it happen too.

I use javascript to create a PNG animation. The issue is that when the page reloads, I need to call that javascript function within the php file which is being accessed by ajax, so the animation plays for the user who didn't click anything. The issue is that I can't call functions (apart from when using onclick) to run in the ajax file, and after googling it says this is impossible anyway. Apart form using eval, which i also couldn't get to work anyway(though apparently is slow and a security risk).

So I'm wondering if there are any solutions to this. All I really need to do is run some javascript in a file being called by ajax. Any ideas are welcome..

Thanks in advance,
Mark.
1:38 pm on Sep 13, 2010 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



I'm not sure I totally understand what you're trying to do. Can you clarify a bit how the JavaScript PNG animation works?

Here's how I would expect this to work.
1. You have a page that loads in the browser, which has the JavaScript functions need to animate a PNG
2. You have an AJAX connection that is polling the server for updates
3. You would have a callback handler for the AJAX method that would determine what logic to take.

For example:

function animateHealing() {
// This code animates some images
}
function animateAttacked() {
// This code animates some images
}
function updateFromServer(data) {
switch(data) {
case 'heal':
animateHealing();
break;
case 'attacked':
animateAttacked();
break;
}
// resume polling
setTimeout(requestUpdate, 0);
}
function requestUpdate() {
$.get('serverUpdates.php', {/*Any data for server*/}, updateFromServer);
}

Note, that's really just sort of a pseudo code example. The details of how often to request updates is not covered in this example (you'd probably also want to look into something like long-polling [en.wikipedia.org].
9:40 am on Sep 16, 2010 (gmt 0)

5+ Year Member



Thanks for you reply.

The issue I'm having, using your example, is how do I get the callback from ajax to run automatically? For example, I can call the function updateFromServer(data) by using <div onclick="updateFromServer(data)"></div>, but how do I make it run automatically when the file is being called through ajax?
1:02 pm on Sep 16, 2010 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



You probably need to use something like Long Polling [en.wikipedia.org] to establish a connection that essentially stays open until the server has information to send.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month