【问题标题】:What is a way to add a delay to a pre-existing redirect code什么是向预先存在的重定向代码添加延迟的方法
【发布时间】:2018-10-11 21:31:02
【问题描述】:

作为一个 完全 javascript 新手(我的经验并没有超越对 CSS 的掌握),有人可以告诉我是否有办法通过添加来为这个确切的重定向代码添加延迟一些东西,你能不能告诉我怎么做,就好像我是一个什么都不知道的婴儿,因为我对 javascript 非常非常缺乏经验,而且非常非常困惑。

<script>
  //redirect to new blog
  var path = window.location.pathname;
  window.location.replace('http://newurl.tumblr.com' + path);
</script>

关于这个主题的所有其他问题似乎都需要比我更强大的 javascript 理解基础,或者他们显示的代码与我正在使用的代码没有太多相似之处,我发现自己感到困惑和当我读到它们时迷路了。像this one 这样的问题确实有看起来很简单的答案,但是由于超时代码中提到了新的 url,我不确定这是否会影响我目前拥有的代码,我更喜欢它,因为它将人们重定向到相应的页面我的博客,而不仅仅是主页。由于这个问题和其他类似的问题让我很困惑,我会感谢任何帮助解决我缺乏经验的问题!

【问题讨论】:

  • 您知道,您提供的代码不涉及 jquery。您链接到的建议答案也没有。
  • 你使用类似setTimeout()
  • 好吧,好吧,这进一步突出了我对 jquery 的困惑和困惑,不是吗哈哈!

标签: javascript redirect delay


【解决方案1】:

你会做setTimeout()

试试这个,看看它是否适合你:

<script>
  //redirect to new blog
  var path = window.location.pathname;
  setTimeout(function(){ 
      window.location.replace('http://belladxne.tumblr.com' + path);
  }, 3000);
</script>

如果我理解正确...不,此代码不应该影响 URL 替换,因为您只是获取当前 URL 的路径名。

【讨论】:

    【解决方案2】:

    使用setTimeout结合您的示例代码和链接问题的建议答案

    <script>
      //delay in seconds:
      var redirectDelay = 5;
    
      //redirect to new blog
      var path = window.location.pathname;
      setTimeout(function() {
            window.location.replace('http://belladxne.tumblr.com' + path);
      }, redirectDelay * 1000);
    </script>
    

    我还添加了变量redirectDelay,以便您可以轻松调整延迟,直到找到适合您想要的延迟时间。

    【讨论】:

      【解决方案3】:

      这里

      setTimeout(function() {
          var path = window.location.pathname;
          window.location.replace('http://belladxne.tumblr.com' + path);
      }, 2000); // <- this is the delay, 2 seconds
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-09
        • 2011-09-20
        • 2012-11-14
        • 2015-09-07
        • 1970-01-01
        • 2021-04-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多