Nilay Singh
Nilay Singh

Reputation: 2331

How to delay response in ajax in Rails

I am trying to delay the ajax success for 1 second in rails ajax . Can anyone suggest me how can i do this in general jquery ajax it is possible but when it comes to using rails ajax I am unable to find away to edit ajax response time . I have the following code for my rails ajax below .

 $(".feed-like-<%= @feed.id %>").html("<%= j render :partial => 'shared/dislike' %>");
    $(".count-<%= @feed.id %>").html('<%= @feed_like_counter.like_count %> ');

How can I add this kind of query :

  // set your delay here, 2 seconds as an example...
                var my_delay = 2000;

                // call your ajax function when the document is ready...
                $(function() {
                    callAjax();
                });

                // function that processes your ajax calls...
                function callAjax() {
                    $.ajax({
                        // ajax parameters here...
                        // ...
                        success: function() {
                            setTimeout(callAjax, my_delay);
                        }
                    });
                }

Upvotes: 1

Views: 391

Answers (1)

user2480754
user2480754

Reputation:

For this particular problem you can use the rails method disable_with you can use in your code like this :

 <%= link_to "like",{ :action => 'create', :controller => 'feed_likes', :feed_id => @feed, :user_id => current_user.id, :remote => true },data: { disable_with: "Processsing..." }, method: :post,class: "btn btn-primary"   %>

Upvotes: 1

Related Questions