I've been faced with a challenge regarding an online chat widget. My goal is to automatically scroll the widget down to display the latest message when a specific chat is opened. Despite using an async
function, I encountered difficulties in achieving this desired functionality.
await axios.post('/', {
id: id
}).then((response) => {
this.messages = response.data;
});
console.log($('#SomeName .messages').height()); // The height returns as zero here, however, it should be much more
$('#SomeName').animate({
scrollTop: $('#SomeName .messages').height()
}, 200);
Upon loading the chat messages, I'm consistently getting a height of 0
for the widget, thus hindering the auto-scroll feature from working as intended. How can I ensure that I wait for the DOM to fully update and obtain the accurate height of the element?
Furthermore, I recognize that the utilization of the await
keyword in this scenario may be unnecessary since there's already a .then()
callback in place. Despite exploring other alternatives, I haven't found a solution yet.