Dienstag, 27. November 2012

Simple cyclic remote link using jQuery

Create a link in the view template you want to trigger a cyclic request:
<%= link_to 'Refresh', root_path, :remote => true, 
  :class => 'auto_remote' %>
I chose the root url as destination, but it should be the route you want to call. Then add a javascript snippet in your application.js (or the js file you use in the view):
$(document).ready(function(){
    jQuery.each($('a.auto_remote'), function(){
     setInterval(function(remote_link) {
        $.ajax({ url: $(remote_link).attr('href') });
      }, 10000, this);
    });
});
In my example the request is triggered every 10th second. Supported by Ruby on Rails 3.2.8 and JQuery 1.8.3

Keine Kommentare:

Kommentar veröffentlichen