homepage Welcome to WebmasterWorld Guest from 54.166.84.82
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Ajax and javascript problem.
Marked

5+ Year Member



 
Msg#: 4201039 posted 7:08 am on Sep 13, 2010 (gmt 0)

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.

 

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4201039 posted 1:38 pm on Sep 13, 2010 (gmt 0)

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].

Marked

5+ Year Member



 
Msg#: 4201039 posted 9:40 am on Sep 16, 2010 (gmt 0)

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?

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4201039 posted 1:02 pm on Sep 16, 2010 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved