0

I need to poll a webservice once an hour to check for new messages. I want to do it with jQuery. The ajax call would be like this:

function checkForMessages(rowid) {
$.ajax({
    type: "POST",
    url: "MyPage.aspx/CheckForMessages",
    contentType: "application/json; charset=utf-8",
    data: "{'currentMessages': '" + cMessages+ "'}",
    dataType: "json",
    success: function (msg) {

    },
    error: function (response) {

    }
});
}

url: "MyPage.aspx/CheckForMessages" is actually a PageMethod instead of Webservice.

What I don't understand is how to make the "loop" with jquery that waits an hour to call the above function.

So how can I make the code that calls this function only once an hour?

Jonathan M
  • 17,145
  • 9
  • 58
  • 91
anmarti
  • 5,045
  • 10
  • 55
  • 96

5 Answers5

0
window.setIntervall(function, delay);

delay in miliseconds.

However, with this method the site must always be open. You could use Server Sent Events to push a message from the server to your site, but this requires a lot of work..

mercsen
  • 692
  • 1
  • 5
  • 21
0

Look at setInterval()...

http://www.w3schools.com/jsref/met_win_setinterval.asp

setInterval(checkForMessages, 3600000);
Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67
0

window.setInterval is the best method for this, obviously it will only poll after an hour if the webpage is still open.

$(function () {
    setInterval(myPollingMethod, 3600000);
});

function myPollingMethod()
{
  //this will repeat every 1 hour (3600 seconds)
}

Also don't forget to call the window.clearInterval method if you want to stop polling.

Steve
  • 3,673
  • 1
  • 19
  • 24
0
function checkForMessages(rowid) {
$.ajax({
    type: "POST",
    url: "MyPage.aspx/CheckForMessages",
    contentType: "application/json; charset=utf-8",
    data: "{'currentMessages': '" + cMessages+ "'}",
    dataType: "json",
    success: function (msg) {
        //do stuff here
        setTimeout(function(rowid) {checkForMessages(rowid);},3600000);
    },
    error: function (response) {

    }
});
}
Jonathan M
  • 17,145
  • 9
  • 58
  • 91
0

You can use setTimeout function in order to achieve the desired result:

function checkForMessages(rowid) {
    $.ajax({
        type: "POST",
        url: "MyPage.aspx/CheckForMessages",
        contentType: "application/json; charset=utf-8",
        data: "{'currentMessages': '" + cMessages+ "'}",
        dataType: "json",
        success: function (msg) {
            ...
        },
        error: function (response) {
            ...
        },
        complete: function() {
            ...
            setTimeout(function() { checkForMessages(rowid); }, 3600000);
        }
    });
}

This way when you call your function for first time and your $.ajax call is finished (regardless if it was success or error) it will schedule to execute the function again after one hour (but please remember that one hour is a lot of time in web world, authorization might expire etc.).

tpeczek
  • 23,867
  • 3
  • 74
  • 77
  • what is the advantage of using setTimeout, and then adding it again once it has elapsed over using setInterval? The latter is cleaner IMO – Steve Jan 14 '13 at 15:39
  • @Steve The differences are very subtle, you can read more about them here: http://stackoverflow.com/questions/729921/settimeout-or-setinterval. In this case they are not relevant but setTimeout is always more reliable to me and because of that it is always my first choice. – tpeczek Jan 14 '13 at 15:48
  • @Archer Fixed the parameter passing. – tpeczek Jan 14 '13 at 15:50
  • @a_maar This is why there is a `setTimeout` call in `complete` callback - this one is called regardless if $.ajax fail or success. – tpeczek Jan 16 '13 at 14:00
  • My `complete` callback is not triggered. Do I have to include both the `success` and `error` as well as `complete` ? – anmarti Jan 16 '13 at 14:02
  • It seems `complete` must go with at least `success`. So I needed to deal with both `complete` and `success`. `complete` to do the *loop* and `success` to do the *stuff* – anmarti Jan 16 '13 at 14:12