What I'm trying to do is detect then collect new PHP array objects (sort of like an api) using ajax/jQuery methods.
{
"posts": [
{
"id": "77",
"post": ""Oh cool bro"",
"time": "Mar 02, 2014"
},
{
"id": "76",
"post": "Ohh",
"time": "Mar 02, 2014"
},
{
"id": "75",
"post": "Yupp",
"time": "Mar 02, 2014"
},
{
"id": "74",
"post": "This is content",
"time": "Mar 02, 2014"
}
]
}
I'm trying to detect a new change in the array with ajax, if a user submits a new post, in real time the array is updated with a post with id 78. I want to be able to detect the addition and eppend only the new post. Additionally, I want the feed to be able to check for new posts every 5 seconds, and append the new posts instead of re-appending all of the posts. Almost exactly like the facebook feed ticker.
My jQuery/ajax code:
function getFeed() {
$.ajax({
type: "GET",
url: "api.php",
dataType: 'json',
success: function(data) {
var posts = data.posts
$.each(posts, function(i) {
$('#posts').append("<div class='post' id='" + posts[i].id + "'>" + "<div class='content'>" + posts[i].post + "</div>" + "<div class='meta'><div class='d'>" + posts[i].time + "</div> - <a href='/edit/" + posts[i].id +"'>Edit</a> - <a href='destroy.php?id=" + posts[i].id + "'>Delete</a></div></div>");
});
}
});
}