Created
June 16, 2016 08:31
-
-
Save mahedi2014/1f0470471e0b9997bc67e8b457f47a1f to your computer and use it in GitHub Desktop.
Facebook like notification reader with ajax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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