I’m trying to dynamically update text on a PHP page via AJAX. But, instead of the text coming through at different intervals, it all comes at once. Maybe this is the wrong approach.
index.html
<html>
<head>
<title>Please Update!</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$.post("hello.php", function(data){
var success = $("#update");
success.css("color", "green");
success.html("> r " + data + ".");
});
});
</script>
</head>
<body>
<div id="update"></div>
</body>
</html>
hello.php
<?php
echo "Hello Worldn";
sleep(1);
echo "rFoobarn";
sleep(1);
echo "rBazboon";
?>
I’d like the text to overwrite itself after one second but it comes barreling down the pipe all at once. ;_;
2
Answers
Not sure about this, but it might help you to do…
in PHP
in Ajax result
Simple Representation
Like Antony suggested. You could create an array in php and encode it as Json
and then create a settimeout().
php
jQuery ~ JavaScript