I am currently working on a Javascript script that automatically scrolls down and loads a new URL when it reaches the bottom of the page. However, I would like to add a delay of 30 seconds before the new URL is loaded.
Although I am relatively new to Javascript, I have managed to create a functioning script. However, I believe there may be an issue with the order or syntax of the code.
While the script successfully scrolls to the bottom of the page, it does not wait for 30 seconds as intended, despite the inclusion of a delay function.
Below is the script I am currently using.
If anyone could assist in rearranging the code to achieve the desired functionality, or provide guidance on how to edit the script appropriately, it would be greatly appreciated. Thank you.
window.scrollTo(0, document.body.scrollHeight);
function delayExecution() {
/**
* This function waits for 30 seconds before launching the next line of code.
*/
setTimeout(function() {
// Code to be executed after the delay
console.log("Next line of code executed after 30 seconds");
}, 30000); // 30 seconds delay
}
// Call the function to delay the execution
delayExecution();
// Function to load a new URL when scroll hits the bottom
function loadNewUrlOnScroll() {
// Check if the scroll has reached the bottom of the page
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
// Replace the URL with the new URL
window.location.href = "https://example.com/new-url";
}
}
// Attach the scroll event listener to the window
window.addEventListener("scroll", loadNewUrlOnScroll);
I attempted to use AI to fix this issue, but it only made things worse, haha.