Ruby Forum Rails Spinoffs (closed, excessive spam) > element timeout after a ajax.updater

Posted by cirpo (Guest)
on 10.04.2007 17:27
(Received via mailing list)
hi, i can't figure out how to solve this problem:
i do a an ajax.updater, it works but i would like that the message
returned by the server "lives" only for 5-10 seconds and then
disappears.(like in Gmail, when you send/delete/etc a message a new
line appears for few second and then it disappears).

this is my function:
function addgr(){
    var gr=$F('form');
    var url= 'page.php';
    var par = 'add=' + gr ;
    var target = $('msg');//the meassage that has to appear for 5-7
seconds
    var addingr = new Ajax.Updater(target,url,{method:
'get',parameters : par});
    }


thanks.

cirpo
Posted by David Dashifen Kees (Guest)
on 10.04.2007 18:02
(Received via mailing list)
Use the onSuccess handler of your Ajax object to set a timeout to remove
the information you added.  For example:

    new Ajax.Updater("message_area", "get_new_message.php", {
        onSuccess: function() { setTimout("clear_message_area", 5000); }
    });


    function clear_message_area() { $("message_area").update(""); }

The onSuccess handler sets a timeout of 5 seconds (the number is in
milliseconds) after which it calls the clear_message_area() function.
That function then simply updates the content of the message_area
element with nothing effectively removing the message from the screen.

 -- Dash --
Posted by cirpo (Guest)
on 10.04.2007 22:24
(Received via mailing list)
thanks!
it works!

i tried to find setTimeout in the docs, but i found nothing...
keep on looking for



thanks again


cirpo
Posted by Christophe Porteneuve (Guest)
on 10.04.2007 23:04
(Received via mailing list)
cirpo a écrit :
> i tried to find setTimeout in the docs, but i found nothing...
> keep on looking for

Navigator DOM.  Try this:

  http://developer.mozilla.org/en/docs/DOM:window.setTimeout

--
Christophe Porteneuve a.k.a. TDD
"[They] did not know it was impossible, so they did it." --Mark Twain
Email: tdd@tddsworld.com
Posted by David Dashifen Kees (Guest)
on 11.04.2007 01:37
(Received via mailing list)
As Christophe indicated, setTimeout isn't part of Prototype.js -- it's
actually just part of JavaScript.  If you search for "javascript
setTimeout" you'll get more information.  There's also a relatively
strong article at about.com regarding the similarities and differences
between setTimeout() and setInterval():

    http://javascript.about.com/library/blstvsi.htm

 -- Dash --
Posted by cirpo (Guest)
on 11.04.2007 08:36
(Received via mailing list)
thanks,thanks,thanks

it's only one month that i started web programming and i'm still
learning...
thanks for your support and sorry for my bad/wrong english.


cirpo
Posted by RobG (Guest)
on 11.04.2007 09:27
(Received via mailing list)
On Apr 11, 9:36 am, David Dashifen Kees <dashi...@dashifen.com> wrote:
> As Christophe indicated, setTimeout isn't part of Prototype.js -- it's
> actually just part of JavaScript.

Nope, it's part of DOM Level 0, it's a method of the window object.


--
Rob
Posted by Yanick (Guest)
on 11.04.2007 16:12
(Received via mailing list)
On 10 avr, 11:26, "cirpo" <alessandro.cine...@gmail.com> wrote:
>                 var par = 'add=' + gr ;
>                 var target = $('msg');//the meassage that has to appear for 5-7
> seconds
>                 var addingr = new Ajax.Updater(target,url,{method:
> 'get',parameters : par});
>                 }
>
> thanks.
>
> cirpo

What you are trying to do is more a problem of server response, you
shouldn't make the client take care of how long a response should
last. The server should be responsible of that task since that it is
it who is responsable of the control flow in your app/Web page. I had
to implement a similar system in my app, and since Prototype is a nice
framework, he will handle whatever the server ask it to do
automatically. Consider this :

var options = {
  method: 'get',
  parameters: par,
  requestHeaders: ['object', 'TestUpdater'],   // optional...
  evalScripts: true    // execute any embedded scripts in the response
};

new Ajax.Updater( 'msg', 'page.php', options );


And a typical response would be :

<div id="msg3434323466792">
<b>Response is : </b>Hello world !
<script type="text/javascript">
  setTimeout( function() { Element.remove('msg3434323466792'); }, 5 *
60 * 1000 );
</script>
</div>


Since we did set 'evalScripts' to 'true', any <script> tag found will
be removed and executed. The response should have, then, a tag with a
very unique Id (usually composed from a timestamp) and the script
executed by the update method of prototype creates a timer that will
remove that tag later on.

This way, you decouple your client from the responsibility of
"cleaning up" the server responses. After all, it doesn't matter from
who, when, or where the request originated ; the client should only
ask question, and handle events, not control the behavior of the
interface.

Hope this helps.

-Yanick
Posted by Tom Gregory (Guest)
on 12.04.2007 06:49
(Received via mailing list)
Yes Rob, you're right.

In David's defense, though, he was perhaps indicating that the
solution could be found in nearly every Javascript book or tutorial
ever written.  Even those from the "dark ages" when Netscape
Navigator was the hottest browser around.

I don't have much faith that someone lacking knowledge of setTimeout
(and, it would seem, the ability to Google it) would understand the
phrase "DOM Level 0."

Besides, he'd be correct in saying the method will exist in every
major modern browser implementation of Javascript.  Cut the guy some
slack.  For most (hobbyist) programmers, the ubiquitously implemented
BOM/DOM standards **are** "Javascript."

I can understand the desire to be precise.  I'm obsessive/compulsive/
passive/aggressive/etc. that way too.

=)


TAG