Skip to content

Instantly share code, notes, and snippets.

@tedshd
Last active August 29, 2015 14:17
Show Gist options
  • Save tedshd/79f08630f92a18aacc0f to your computer and use it in GitHub Desktop.
Save tedshd/79f08630f92a18aacc0f to your computer and use it in GitHub Desktop.
polling with jquery
var polling,
pollingResponse = true;
polling = setInterval(
function () {
if (pollingResponse) {
console.log('polling');
pollingResponse = false;
$.ajax({
url: 'test.php',
type: 'POST',
data: {
// data
},
timeout: 5000,
success: function(data) {
if (data.status === 'ok') {
// do something
clearInterval(polling);
} else {
// do something
}
pollingResponse = true;
},
error: function(data) {
// do something
clearInterval(polling);
pollingResponse = true;
}
});
}
},
5000
);
skip3minPolling = setTimeout(function() {
clearInterval(polling);
// show notification to view
}, 180000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment