I'm working on improving my chat script using jQuery to display and autoscroll the messages. It already works pretty good, but I want to get rid of the 'refresh' effect when replacing the 'div' with updated content from the JSON object, which gets encoded from a PHP array containing the messages with every jQuery request.
How can I achieve this?
This is what works now for me:
<head>
<script>
$(document).ready(function(){
$.ajaxSetup({cache:false});
$("a").click(function(){
$("div").empty(); // removing this will get rid of the 'refresh' effect, but it endlessly appends the message log
$.getJSON('ajax.php', function(data){
$.each(data, function(i, field){
$("div").append(field + "<br>");
});
$(document).scrollTop($(document).height());
});
});
});
window.onload = function() {
document.getElementsByClassName("refreshB")[0].click();
};
var intervalID = window.setInterval(refresh, 10000);
function refresh() {
document.getElementsByClassName("refreshB")[0].click();
}
</script>
</head>
<body>
<a class="refreshB"></a>
<div></div>
</body>