如何在url引用脚本中添加5秒等待?

时间:2013-02-02 05:33:54

标签: javascript

我有url重定向脚本或url引用脚本 我想添加5秒等待,然后url重定向到所需的URL。

 <script>
 var url_index = window.location.href.indexOf('url=');
 if (url_index != -1)
 {
    url_go = window.location.href;
    url_go = url_go.substring(url_index + 4);
    //document.write(url_go);
    window.location.href = url_go;
 }
 var $_ = function(x){if(!document.getElementById){return;} else{ return document.getElementById(x);}}
 </script>  

输出为http://transistortechnology.blogspot.com url = http://imgur.com

当我打开上面的url然后突然重定向,但我想添加5秒等待。

是否可以在上面的脚本代码中。

我将该脚本放在博客标题部分

3 个答案:

答案 0 :(得分:1)

请参阅:Javascript - Wait 5 seconds before executing next line
基于此,以下应该可行:

 url_go = ""
 function doRefer() {
     var url_index = window.location.href.indexOf('url=');
     if (url_index != -1)
     {
        url_go = window.location.href;
        url_go = url_go.substring(url_index + 4);
        setTimeout(function () {window.location.href = url_go;}, 5000);
     }
 }

答案 1 :(得分:0)

是的,这很容易做到。你可以通过调用setInterval来实现。 Here is some documentation on how to do it.

以下是一些代码(来自网站):setInterval(function(){alert("Hello")},3000);

答案 2 :(得分:-1)

<script>
   var count = 15; // Number of seconds to wait
   var counter; // Handle for the countdown event.

   function start() {
    counter = setInterval(timer, 1000);
   }

   function timer() {
    // Show the number of remaining seconds on the web page.
    var output = document.getElementById("displaySeconds");
    output.innerHTML = count;

    // Decrease the remaining number of seconds by one.
    count--;

    // Check if the counter has reached zero.
    if (count < 0) { // If the counter has reached zero...
     // Stop the counter.
     clearInterval(counter);

     // Start the download.
     window.location.href = "URL to open";
//if you want to open in new window then use window.open(url);
     return;
    }
   }  

   window.addEventListener("load", start, false);
  </script>

  <div>
  Your download will begin in <span id="displaySeconds">15</span> seconds.<br />
</div>

感谢technovedant提供此脚本。我在他的博客上找到了