1

嘿,我怎样才能隐藏我的下载链接,并做一个倒计时类型的东西。也许让它从 10 开始倒数,一旦完成,就会出现下载链接,最好用 js 来做,对吧?

有谁知道如何做到这一点?:D

谢谢

4

4 回答 4

2

完整示例:

<span id="countdown"></span>
<a id="download_link" href="download.zip" style="display:none;">Download</a>
<noscript>JavaScript needs to be enabled in order to be able to download.</noscript>
<script type="application/javascript">
(function(){
   var message = "%d seconds before download link appears";
   // seconds before download link becomes visible
   var count = 10;
   var countdown_element = document.getElementById("countdown");
   var download_link = document.getElementById("download_link");
   var timer = setInterval(function(){
      // if countdown equals 0, the next condition will evaluate to false and the else-construct will be executed
      if (count) {
          // display text
          countdown_element.innerHTML = "You have to wait %d seconds.".replace("%d", count);
          // decrease counter
          count--;
      } else {
          // stop timer
          clearInterval(timer);
          // hide countdown
          countdown_element.style.display = "none";
          // show download link
          download_link.style.display = "";
      }
   }, 1000);
})();
</script>
于 2011-03-05T21:53:58.387 回答
1

看一下setTimeout函数。您可以执行以下操作:

function displayLink() {
  document.getElementById('link_id').style.display = 'block';
}

setTimeout(displayLink, 10000);
于 2011-03-05T21:38:18.460 回答
1

您可以为此使用 setInterval。setInterval 的行为类似于计时器,您可以在其中定期运行某个函数。这样的事情应该做的工作(未经测试):

$(".link").hide();

var iteration = 0;
var timer = setInterval(function() {
    if(iteration++ >= 10) {
        clearTimeout(timer);
        $(".link").show();
        $(".counter").hide();
    }

    $(".counter").text(10 - iteration);
}, 1000);

这最初将隐藏下载链接并每秒运行一个函数,从 10 开始倒计时。当我们收到 10 时,我们隐藏计数器并显示链接。使用 ClearTimeout 是为了让我们在达到 10 后不计算在内。像戴尔一样简单。

编辑:正如评论中提到的,这个函数使用 jQuery 来查找元素。

于 2011-03-05T21:44:06.177 回答
0
var WAIT_FOR_SECONDS = 10;
var DOWNLOAD_BUTTON_ID = "btnDownload";

if (document.body.addEventListener) {
    document.body.addEventListener("load", displayDownloadButton, false);
} else {
    document.body.onload = displayDownloadButton;
}

function displayDownloadButton(event) {
    setTimeout(function() {
        _e(DOWNLOAD_BUTTON_ID).style.display = "";
    }, WAIT_FOR_SECONDS*1000);
}

function _e(id) {
    return document.getElementById(id);
}
于 2011-10-05T17:22:10.933 回答