0

I'm trying to do something with javascript to let my chat behave a little better than how it does now.

Since I use PrimeFaces' poll to update the panel and simulate the chat, the panel gets updated each second, making really hard to read an old message (updating the panel causes the scroll to be reset at the top of the panel).

So I've added an onscroll event to the div, to stop polling when the user is scrolling.

But I need a way to make it restart, so I've thought of something like putting a timer and recall the poll.start() a minute after the stop of the scrolling.

Is there any way to do that?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
StepTNT
  • 3,867
  • 7
  • 41
  • 82
  • Sure... what exactly are you having problems with? – Felix Kling May 01 '12 at 14:45
  • The only problem is that there isn't and `onScrollStop` event in javascript, so I don't know how to handle this thing :) – StepTNT May 01 '12 at 14:52
  • possible duplicate of [jQuery: Event, when User stops scrolling](http://stackoverflow.com/questions/3701311/jquery-event-when-user-stops-scrolling) -- although jQuery is mentioned, the accepted answer is applicable without jQuery. And it's the only solution afaik. – Felix Kling May 01 '12 at 15:00

1 Answers1

1

What I've done on my own chat engine is this:

When a new message arrives...

  • If the user is scrolled all the way to the (new message) end of the view, keep the scroll anchored at that end.
  • Otherwise, keep the scroll where it was before.

This can easily be tested by comparing the value of scrollTop with either 0 or scrollHeight-offsetHeight.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • I don't know if this can be done with PrimeFaces, because polling updates the whole panel, causing the div to be "refreshed". Your solution seems good but with push and not with poll. With push you can make an `onMessage` method that does what you said, but my poll updates the panel without checking it new messages are there..it's more like a refresh each second, so I don't have a lot of control on it! – StepTNT May 01 '12 at 14:52
  • Hmm... in that case, maybe you can have the polling function save the current scroll position before updating the div, then set the scroll position back after the update. – Niet the Dark Absol May 01 '12 at 20:56