Skip to content

Instantly share code, notes, and snippets.

@mahedi2014
Created June 16, 2016 08:31
Show Gist options
  • Save mahedi2014/1f0470471e0b9997bc67e8b457f47a1f to your computer and use it in GitHub Desktop.
Save mahedi2014/1f0470471e0b9997bc67e8b457f47a1f to your computer and use it in GitHub Desktop.
Facebook like notification reader with ajax
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
var num =1;
$(document).ready(function() {
setInterval(longPoll,8000);
});
function longPoll() {
var shouldDelay = false;
$.ajax({
url: 'http://localhost',
async: true, // by default, it's async, but...
dataType: 'html', // or the dataType you are working with
timeout: 10000, // IMPORTANT! this is a 10 seconds timeout
cache: false
}).done(function (data, textStatus, jqXHR) {
// do something with data...
//$.sticky(num++);
$('#notification').html(num++);
}).fail(function (jqXHR, textStatus, errorThrown ) {
shouldDelay = textStatus !== "timeout";
}).always(function() {
// in case of network error. throttle otherwise we DOS ourselves. If it was a timeout, its normal operation. go again.
var delay = shouldDelay ? 100000: 0;
window.setTimeout(longPoll, delay);
});
}
</script>
</head>
<body>
<span id="notification">0</span>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment