Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Ajax chat question

Featured Replies

so i have this function which gets called using setInterval to update the chat box with new messages every 2.5 secs

 

the problem is the part that scrolls to the bottom of the div when i submit a new message , it works fine however because its getting called every 2.5 secs if a person is scrolling up the div with there mouse to look at old messages it will end up scrolling to the bottom automatically, how can i fix this? i've seen other examples where there doing the exact same thing and it gets called every 2 or 3 secs but they dont have this same problem. thanks in advance

 

function LoadLog(){
   var oldscrollHeight = $("#Chat_Box").attr("scrollHeight") - 20; //Scroll height before the request  
$.ajax({
 url:"./ajax/chat_update.php",  
 cache: false,
 success:function(html){
   //Update chat box
   $("#Chat_Box").html(html);	
   var newscrollHeight = $("#Chat_Box").attr("scrollHeight") - 20; //Scroll height after the request  
           if(newscrollHeight > oldscrollHeight){  
           $("#Chat_Box").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
           } 
   }
});

Edited by webdesigner93

You're already getting their current scroll height before the request. Couldn't you just do a quick check to see if they're at the bottom of the div or not. If they are; update and auto-scroll to the bottom, if they're not just update and leave their scroll where it is?

  • Author

You're already getting their current scroll height before the request. Couldn't you just do a quick check to see if they're at the bottom of the div or not. If they are; update and auto-scroll to the bottom, if they're not just update and leave their scroll where it is?

 

mmm i got this now

 

var newscrollHeight = $("#Chat_Box").attr("scrollHeight") - 20; //Scroll height after the request  
     if ($("#Chat_Box").scrollHeight - $("#Chat_Box").scrollTop() == $("#Chat_Box").outerHeight()) {
           if(newscrollHeight > oldscrollHeight){  
           $("#Chat_Box").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
           } 
		}

 

the problem now is on the chat window of the person u sending the message to when they get that message it moves the scroll bar up a tad hiding part of the incoming message to them

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.