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?

Edited by SocialCloud
Link to comment
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.

Edited by SocialCloud
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.