我正在编写一个 PHP 代码来从我的服务器 ping 一台计算机。代码很简单,但我认为浏览器正在缓存传入的数据:
运行.php
<?php
for ($i=0; $i<10; $i++) {
$host = '127.0.0.1';
$port = 80;
$waitTimeoutInSeconds = 1;
if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)){
// It worked
echo "yes, you can access $ip from this server<BR>";
sleep(1);
} else {
// It didn't work
echo "nope, you cannot access $ip from this server<BR>";
}
fclose($fp);
}
?>
索引.php
<script src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
$.ajax({
url: 'run.php',
success: function(data) {
$('.ping-data').append(data);
}
});
</script>
Ping data:
<div class="ping-data" style="border: 1px solid #9f9f9f; width: 600px; min-height: 400px;"></div>
但是当我运行代码 index.php 时,它会等待并回显整个数据一次,而不是每秒打印一行 ping。每当发送一行数据时,如何捕获此事件并打印数据?