Reputation: 21739
<?
if($_POST['begin'])
{
while(1)
{
echo "1";
sleep(2);
}
die();
}
?>
<span class="answer"></span>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "exp.php",
data: "begin=1",
success: function(msg){
$(".answer").html(msg);
}
})
})
</script>
It, of course, doesn't work. What should I change to make it work? Can I avoid using setInterval, setTimeout or other functions in javascript?
By the way, what I am trying to do here is to write number 1 every two seconds.
Upvotes: 0
Views: 268
Reputation: 41664
This is the wrong approach, because your success function isn't going to run until it receives a response from the server which isn't going to happen as it's in an endless loop.
You will need to handle the timings in JavaScript with, as you say, setInterval. Of course if all you're trying to do is simply print the number 1 every five seconds you don't need to make any calls to the server-side (although I presume there's something more you're trying to achieve eventually - you might want to expand on that a little).
You could look into opening a WebSocket back to the server do handle this kind of ongoing communication. Look into something like PusherApp - http://pusher.com/
Upvotes: 0
Reputation: 145482
I've never tried it, but the XMLHttpRequest interface supposedly supports streamed requests. In particular there is the .readyState==3
which denotes partial results (See spec http://www.w3.org/TR/XMLHttpRequest/#event-handler-attributes).
When you don't want to set an interval handler, then you will have to override the actual XHR callback, because the jQuery success:
will really only fire on completion.
xmlHttp = $.ajax({ ... });
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState >= 3) {
alert(xmlHttp.responseText);
}
};
Note that the responseText will always contain the accumulated data. So you have to split it up on \n
or something, if you want to read the latest 1
.
Upvotes: 2
Reputation: 57709
Ok, I think I know what you want. It's weird .. but fine.
<script type="text/javascript">
$(document).ready(function() {
function fetch_a_one() {
$.ajax({
type: "POST",
url: "exp.php",
data: "begin=1",
success: function(msg){
$(".answer").html(msg);
fetch_a_one();
}
})
}
fetch_a_one();
})
</script>
<div class="answer"></div>
PHP script:
<?php
sleep(2); // way two seconds
exit(1); // print 1
?>
Minus some delay from starting the Ajax request and lag from the server, this should print '1' every 2 seconds .. no idea why you want this.
Upvotes: 1
Reputation: 3298
I think the problem is, that you're writing endless PHP loop.
jQuery when starting ajax request, will be waiting for script to finish his job. However the script would never ends - so the browser will never get the full answer.
You need to use setTimeout, there is no other way - at least no other easy and safe way.
Upvotes: 0
Reputation: 218828
You're probably going to have to use JavaScript for this. You can continually poll the server resource to get information from it, but the actual looping and delaying will need to be in JavaScript.
The reason for this is because the PHP script needs to finish processing at some point. It's not streaming output to the client, it's building output to send to the client. In the code you provide, it's forever building the output and never sending it.
You can try to flush your buffer from the PHP script to send some output to the client while still building more output, but take a look at the caveats in that link. It's not really a clean way to accomplish this and will probably cause more problems than it solves in this case. At some point the server resource needs to stop processing and commit to a response. Trying to short-circuit that basic concept of HTTP will likely be a bit of a hack.
Upvotes: 0