Jump to content

Ajax Jquery Chat


MDCode

Recommended Posts

Ok so I'm new to ajax/jquery and am making a basic chat thingy . Yea. So I want it to scroll down to the last message in the div, which works, but a user can not scroll up the way I'm using it because of the interval refresh at the moment.

 

<script>
var load = setInterval(
function()
{
$('#chat_container').load('fetchchat.php');

var objDiv = document.getElementById("chat_container");
objDiv.scrollTop = objDiv.scrollHeight;

}, 1000);
</script>

 

Any idea where I could move it that would not affect the user's scrolling?

Link to comment
https://forums.phpfreaks.com/topic/270657-ajax-jquery-chat/
Share on other sites

I tried your suggestion...and failed epically. But! I google'd my hands to numbness and found a solution. If anyone is interested, here it is :)

 

<script>
var isScrolling = false;
var load = null;
$(function() {

$('#chat_container').on('scroll', function() {
isScrolling = true;
if (load != null) {
clearInterval(load);
load = null;
}
});

var pollScrolling = setInterval(function() {
isScrolling = false;

if (load == null) {
load = setInterval(refreshContent, 5000);
}
}, 500);

load = setInterval(refreshContent, 300);

function refreshContent() {
if (!isScrolling) {

$('#chat_container').load('fetchchat.php');
var objDiv = document.getElementById("chat_container");
objDiv.scrollTop = objDiv.scrollHeight;
}
}

});
</script>

 

Just checking if the user is scrolling, if they stop scrolling for 5 seconds, send them back to the bottom.

Link to comment
https://forums.phpfreaks.com/topic/270657-ajax-jquery-chat/#findComment-1392329
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.