Page 1 of 1

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

Posted: Sat Feb 23, 2008 4:41 pm
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.

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

Posted: Mon Feb 25, 2008 10:36 am
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?

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

Posted: Mon Feb 25, 2008 11:14 am
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...

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

Posted: Mon Feb 25, 2008 11:18 am
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?

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

Posted: Mon Feb 25, 2008 11:28 am
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...

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

Posted: Mon Feb 25, 2008 11:44 am
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.

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

Posted: Mon Feb 25, 2008 11:50 am
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...

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

Posted: Mon Feb 25, 2008 12:04 pm
by pickle
Is the Form.serialize() where it pulls the currently selected value?

Also, please use [syntax=javascript][/syntax] tags when posting Javascript code.

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

Posted: Mon Feb 25, 2008 12:09 pm
by legend986
Sorry about that. I just missed it. And yes, the serialize function grabs the presently selected value.

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

Posted: Mon Feb 25, 2008 12:21 pm
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?

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

Posted: Mon Feb 25, 2008 12:39 pm
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.