Skip to content

Instantly share code, notes, and snippets.

@phillipadsmith
Last active December 19, 2015 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipadsmith/e59234caa52830dee15b to your computer and use it in GitHub Desktop.
Save phillipadsmith/e59234caa52830dee15b to your computer and use it in GitHub Desktop.
Long-poll pattern for a "stream" of updates...
<script>
$().ready(function () {
var url = 'http://api.domain.com/v1/latest/blogs/?callback=?';
$.getJSON( url, function (data) {
$.each(data.hits.hits.slice(0,9), function (index, value) {
var story = value._source;
var storyDate = story.storyDate + ' -0700';
var date = moment( storyDate).fromNow();
console.log( story.storyDate );
console.log( date );
var li = $('<li>')
.attr('id', 'story-'+index)
.append($('<a>')
.attr('href', story.uri)
.attr('title', story.title).text(story.title))
.append($('<br /><span class="date">')
.text( 'Published ' + date ));
$('#election-hook-frontpage ul.links').append(li);
});
});
});
(function poll(){
var url = 'http://api.domain.com/v1/latest/blogs/?callback=?';
$.ajax({ url: url, success: function(data){
$.each(data.hits.hits.slice(0,9), function (index, value) {
var story = value._source;
var storyDate = story.storyDate + ' -0700';
var date = moment( storyDate).fromNow();
console.log( story.storyDate );
console.log( date );
var li = $('<li>')
.attr('id', 'story-'+index)
.append($('<a>')
.attr('href', story.uri)
.attr('title', story.title).text(story.title))
.append($('<br /><span class="date">')
.text( 'Published ' + date ));
$('#election-hook-frontpage ul.links li#story-'+index).replaceWith(li);
});
}, dataType: "json", complete: function() { setTimeout(poll, 60000); } });
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment