Alan Coromano
Alan Coromano

Reputation: 26008

Periodically send ajax requests

There is a page and I want periodically to make "background" ajax requests. So the page is loaded then it should send ajax requests in a certain amount of time.

I might use cron for that. I have never use previously so I'm wondering if it would fit for that task. Is there any other more simple way?

P.S. The time delay will be about 5 minutes.

Upvotes: 4

Views: 8610

Answers (5)

Bitterzoet
Bitterzoet

Reputation: 2792

Cron is run on the serverside and you are using HTML and AJAX, so you should solve this issue in Javascript :-)

By using something like setInterval you can keep executing a function, your case might be something like polling a url via AJAX:

function updatePage(){
  // perform AJAX request
}
setInterval(updatePage, 5000);

Upvotes: 5

Richard Neil Ilagan
Richard Neil Ilagan

Reputation: 14747

Since there is essentially an unknown delay between the time you send out an AJAX request and the time you receive a complete response for it, an oftentimes more elegant approach is to start the next AJAX call a fixed amount of time after the prior one finishes. This way, you can also ensure that your calls don't overlap.

var set_delay = 5000,
    callout = function () {
        $.ajax({
            /* blah */
        })
        .done(function (response) {
            // update the page
        })
        .always(function () {
            setTimeout(callout, set_delay);
        });
    };

// initial call
callout();

Upvotes: 6

Jesus Anaya
Jesus Anaya

Reputation: 46

Are you using jquery? If so, you can implement this method:

// first, you need asing a callback timer
var timeout = 300; //milliseconds

// this method contain your ajax request
function ajaxRequest() { //function to ajax request
    $.ajax({
        url: "/url/to/request/"
    }).done(function(data) {
        alert("response is: " + data);
    });
}


$(document).on("ready", function(){

    //this method will be called every 300 milliseconds
    setInterval(ajaxRequest, timeout);
});

Upvotes: 1

Ganesh Kunwar
Ganesh Kunwar

Reputation: 2653

You can send ajax request in four second like this:

setInterval(get_news, 4000);
        function get_news(){
             $.ajax('/dashboards/get_news', {
                type: 'POST',
                success: function(result) {
                    if(result > 0){
                        $('#div_1').text("See "+result+" new messages");
                        $('#div_1').show();
                    }
                    else{
                        $('#div_1').css('display', 'none');
                    }
                     },
                error: function() {
                    // alert("Error")
                }
            });       

        }

Upvotes: 1

Matt
Matt

Reputation: 14038

Depending on your rails version you may be able to use periodically_call_remote, otherwise you'll need the jquery alternative that @Bitterzoet described.

More info in this question.

Upvotes: 1

Related Questions