如何将这个脚本整理好?

huangapple go评论73阅读模式
英文:

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);

huangapple
  • 本文由 发表于 2023年7月23日 19:43:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定