4

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.

Alan Coromano
  • 24,958
  • 53
  • 135
  • 205

5 Answers5

6

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();
Richard Neil Ilagan
  • 14,627
  • 5
  • 48
  • 66
5

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);
Bitterzoet
  • 2,752
  • 20
  • 14
  • Elaborating @stecb's comment, do your best to never use the `eval` version of `setInterval()` ans `setTimeout()`. Always use a function directly, either by reference or by a direct lambda. – Richard Neil Ilagan Apr 18 '13 at 12:08
  • Right @RichardNeilIlagan :) I was with mobile.. ty ;) btw, it's also a matter of scope, passing it as a string will set a global scope for that foo. – stecb Apr 18 '13 at 13:34
1

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.

Community
  • 1
  • 1
Matt
  • 13,948
  • 6
  • 44
  • 68
1

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")
                }
            });       

        }
Ganesh Kunwar
  • 2,643
  • 2
  • 20
  • 36
1

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);
});
Ren
  • 1,111
  • 5
  • 15
  • 24