I'm doing a Periodic Update but it looks like Incremental U

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

I'm doing a Periodic Update but it looks like Incremental U

Post by legend986 »

I have written a small script using prototype. I have two drop down boxes. The choices in the second one depend on the first one. So I use ajax to get the choices and populate the boxes. When the second box is selected, I wrote a function to initiate a Ajax.PeriodicalUpdater call to refresh a div tag every few seconds. So far, so good. Everything is working. I'm able to see an image(I displayed it) refresh itself.

Now, when I select another option from the second box, the new image is appearing but the old one is coming too. I mean, first the old one comes, then refreshes into the second one. I've been trying to get over this problem but in vain. Can someone please help me out? Just in case, my problem is something like:

Once I click on a submit button, I need to send the request to a php file which will give me an image file which i'm displaying. Now, I need to keep sending the POST data every few seconds so that the php file generates a new image and gives it back. This image should be displayed here on this page.
Last edited by legend986 on Mon Feb 25, 2008 10:37 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: I'm doing a Periodic Update but it looks like Incremental U

Post by pickle »

How are you updating the image? Is your AJAX call giving you a full image tag each time? Is the image name even changing or is it just the content of the image?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

Re: I'm doing a Periodic Update but it looks like Incremental U

Post by legend986 »

Oh... The image tag is changing. I set the Ajax.PeriodicalUpdater property Insertion to Top and it kept inserting new images so i guess it is receiving new images but at the same time initiating even the old Ajax.PeriodicalUpdater calls...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: I'm doing a Periodic Update but it looks like Incremental U

Post by pickle »

I'm not too versed in the prototype JS library, but maybe there's a place where you've added listeners more than once, or you haven't stopped listening at some point?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

Re: I'm doing a Periodic Update but it looks like Incremental U

Post by legend986 »

Well, the problem as I see is that the periodical updater (this function is something that initiates the ajax call every few seconds) is doing its job but when I click on a new selection, the old call is remaining even though I stop it using a stop method that is provided. So, I guess the best method would be to actually refresh the page and accept the choice again...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: I'm doing a Periodic Update but it looks like Incremental U

Post by pickle »

Or maybe change the periodic update to retrieve the value that is selected, every time it does the ajax call, rather than (I'm guessing) be initialized with a particular value.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

Re: I'm doing a Periodic Update but it looks like Incremental U

Post by legend986 »

Well, actually I think I did something like that but as it didn't work, I wrote the following:

Code: Select all

 
    function send_final(x) {
        var params = Form.serialize($('nodes'));
          updater = new Ajax.PeriodicalUpdater('updateDiv2', 'spiderPlot.php', {
                                                                       asynchronous:true, 
                                                                       frequency:2,
                                                                       decay:1, 
                                                                       onComplete:function(){ 
                                                                                                            new Effect.Highlight('updateDiv2');},
                                                                                                           //insertion: Insertion.Top,
                                                                                                           parameters:params});
        if(x==1)
                updater.start();
    }
 
    function stopupdater() {
        updater.stop();
        updater.stop();
    }
So I solved this problem by placing two links: One is a Submit link that calls the send_final function and the other is a "Click me a few times to stop the image loading and then click on the Submit again" link to call the stopupdater() a few times (I'm really not sure why the function is not stopping when I call updater.stop() once. )So far it seems like it is working but it is really weird on my part to give control of stop logic to the end user...
Last edited by legend986 on Mon Feb 25, 2008 12:11 pm, edited 2 times in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: I'm doing a Periodic Update but it looks like Incremental U

Post by pickle »

Is the Form.serialize() where it pulls the currently selected value?

Also, please use [syntax=javascript][/syntax] tags when posting Javascript code.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

Re: I'm doing a Periodic Update but it looks like Incremental U

Post by legend986 »

Sorry about that. I just missed it. And yes, the serialize function grabs the presently selected value.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: I'm doing a Periodic Update but it looks like Incremental U

Post by pickle »

Is send_final() being called more than once? It looks once you call it, it executes periodically all on it's own. Is there a way to only call it once when the pulldown is first changed, then never call it again?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

Re: I'm doing a Periodic Update but it looks like Incremental U

Post by legend986 »

Well, I'm not sure how to do that. I've added an event to a link besides the drop down. So when the user selects and option and then click on the link, it calls the send_final function. So you want me to hide the link after clicking on it once? If yes, then how would I support future selections? For instance, the user wants to see some other graph and so is required to select another option from the graph.
Post Reply