Skip to content

Instantly share code, notes, and snippets.

@stmllr
Created November 15, 2012 23:41
Show Gist options
  • Save stmllr/4082423 to your computer and use it in GitHub Desktop.
Save stmllr/4082423 to your computer and use it in GitHub Desktop.
Poll song name from remote service using jQuery
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.js"></script>
<div id="song">Loading song...</div>
$(document).ready(function() {
$.post(
'http://example.com/get/song',
function(data) {
if (data.song !== '') {
$('#song').text(data.song);
}
}
);
(function poll() {
setTimeout(function(){
var songRequest = $.post('http://example.com/get/song');
songRequest.success(function(data) {
if (data.song === '') {
var song = 'Service temporarily unavailable';
} else {
var song = data.song;
}
$('#song').text(song);
poll();
});
songRequest.error(function(data) {
$('#song').text('Service temporarily unavailable');
poll();
});
}, 3000);
})();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment