英文:
How to put this script in order?
问题
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("等待30秒后执行下一行代码");
// 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);
}, 30000); // 30秒的延迟
}
// 调用函数延迟执行
delayExecution();
英文:
I have a script in Javascript that scrolls down and loads a new url when it hits the bottom. Now, I want the script to wait for 30 seconds when it hits the bottom, and then load the new url.
I am quite new to Javascript, the code I have works, but I think the order or syntax is wrong.
It does scroll to the bottom but once it hits the bottom it doesn't wait for 30 sec, altough I have the wait function in the script.
This is my current script.
If someone can help me reorder it so it works as intended, or help me edit the script, would be great. Thanks.
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 tried fixing this via AI but it made it worst, lol.
答案1
得分: -1
window.scrollTo(0, document.body.scrollHeight);
function delayExecution() {
/**
* 此函数在启动下一行代码之前等待30秒。
*/
setTimeout(function () {
// 用新的URL替换URL
window.location.href = "https://example.com/new-url";
}, 30000); // 30秒延迟
}
// 当滚动到页面底部时加载新URL的函数
function loadNewUrlOnScroll() {
// 检查滚动是否已经到达页面底部
if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
// 调用延迟执行函数
delayExecution();
}
}
// 将滚动事件监听器附加到窗口
window.addEventListener("scroll", loadNewUrlOnScroll);
英文:
window.scrollTo(0, document.body.scrollHeight);
function delayExecution() {
/**
* This function waits for 30 seconds before launching the next line of code.
*/
setTimeout(function () {
// Replace the URL with the new URL
window.location.href = "https://example.com/new-url";
}, 30000); // 30 seconds delay
}
// 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) {
// Call the function to delay the execution
delayExecution();
}
}
// Attach the scroll event listener to the window
window.addEventListener("scroll", loadNewUrlOnScroll);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论