Let's get straight to the point.. I have got 2 containers
, each with a height of 100vh
. The second container is supposed to be displayed when a button in the first container is clicked. However, the issue arises when the button click event function also includes the line
window.scrollTo(0,document.body.scrollHeight);
, causing the scroll to jump to the bottom prematurely. This happens because JavaScript is asynchronous
, and it doesn't wait for the information from an API (which takes some time) to load before executing the scroll function. How can I resolve this problem?
Edit: Here are some additional details.. The visibility of the second container depends on a boolean variable which is set to true in the callback function.
This is how I achieve that:
.then(res => {
res.forEach(data => {
...
}
if (Object.keys(res).lenght > 1) {
this.isTrack = false
this.displayTracks = true <!-- Second container's boolean variable -->
window.scrollTo({top: 5000, behavior: 'smooth'});
}
})