So, I've been trying to do Long-Polling using the jQuery Library and PHP. I'm doing this so I can make some sort of real-time notifications system in the future. The code I have now isn't really working.
index.php
<html>
<head>
<title>Long Polling</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.6.2.min.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
getData();
});
function getData() {
$.ajax({
type: "POST",
url: "ajax.php",
async: true,
timeout: 50000,
data: "get=true",
success: function(data) {
$("#info").append(data);
setTimeout("getData()", 1000);
}
});
}
</script>
</head>
<body>
<div id='info'></div>
</body>
</html>
Ajax.php
<?php
if(rand(1, 100) % 2) {
echo 'even';
} else {
sleep(rand(1, 4));
}
?>
even
. – Bronchitisif(rand(1, 100) % 2)
returns true I want the request to be held open waiting for it to return true upon the next try. – Fanestil