【问题标题】:Delay automatic url redirect with jquery?用jquery延迟自动url重定向?
【发布时间】:2011-04-10 07:28:30
【问题描述】:

我需要一个转换页面,它会在自动重定向到主页之前显示 2 秒。我该怎么做呢?我似乎不能耽误为我工作。

【问题讨论】:

  • 你使用 jQuery 1.4.2 吗?因为旧版本还不支持 delay()。

标签: jquery url redirect delay


【解决方案1】:

你可以直接使用setTimeout(),像这样:

setTimeout(function() {
  window.location.href = "/NewPage.aspx";
}, 2000);

【讨论】:

    【解决方案2】:

    您可以使用jQuery Timer。这是代码(也可以找到in this article):

    // This will hold our timer
    var myTimer = {};
      // delay 2 seconds
      myTimer = $.timer(2000, function() {
    
      //redirect to home page
      window.location = "/RedirectTimer/Home.aspx";
    });
    

    【讨论】:

    • 好答案,只要考虑是否值得标记,因为它会等待 3 秒,而不是请求的 2 秒;)
    • @Paul - 哈哈...嗯...我在展示 cmets 的力量。看……你马上就发现了这个错误,因为代码很容易阅读! ;-) :-p (我将答案更新为两秒钟。)
    【解决方案3】:
    setTimeout(function(){ window.location = "/NewPage.aspx"; }, 2000);
    

    【讨论】:

      【解决方案4】:

      the delay() function 不适合你吗?带有 setTimeout() 的 Vanilla JavaScript 也同样适用。

      提示:如果不显示当前代码,建议实际代码有点困难。

      【讨论】:

      • 我尝试了您的建议,但对我不起作用。我做了一些调整,也没有奏效。这是我所在的位置: <script type="text/javascript"> $(document).ready(function(){ alert('works!'); // 这将保存我们的计时器 med = " <a href="http://www.zhospitalitygroup.com/mediterraneo/press/" rel="nofollow" target="_blank">zhospitalitygroup.com/mediterraneo/press"; var myTimer = {}; // 延迟 3 秒 myTimer = $.timer(1000, function() { //重定向到主页 document.location = med; }); }); </script>
      • @mark:我认为您的评论是为了@JasCav 的回答。代码格式化通过 cmets 中的反引号起作用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多