0

I'm building a simple PhoneGap application where I get new messages from MYSQL server and show them to the user. The application is supposed to get a JSON like this

 {
    "key": [
        {
            "Message": "test"
        }
    ]
}

from server every minute and alerts if there is something new. But right now it alerts message "test" every time even though the JSON has changed to this:

{"key":[]}

after the application has asked it from the server the first time.

This is the JS I use

 setInterval ( "getMessages()", 60000 );
function getMessages() {
    $.getJSON(serviceURL + 'messages.php' ,function(result){
    $.each(result.key, function(index, field){
    alert(field.message);
  });
});
}
2
  • Could it be because field is undefined so it uses the previous one?
    – Mosselman
    Jun 5, 2012 at 7:19
  • is your always json containing an array of one element ? Or could you have more than one key/object inside? Jun 5, 2012 at 7:19

1 Answer 1

0

The request is probably being cached by your browser. Change your url to:

serviceURL + 'messages.php?' + new Date().getTime()

this will trick the browser into thinking it's a different request each time and therefore avoids issues with caching.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.