1
A
回答
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倒計時當我們reaced十個,我們躲在櫃檯,並顯示該鏈接。我們使用了ClearTimeout,以便在我們達到10之後不計算在內。容易如戴爾。
編輯:正如在評論中提到的,這個函數使用jQuery來查找元素。
+0
你應該補充說這段代碼使用了jQuery。 – Lekensteyn 2011-03-05 21:44:47
+0
@Lekensteyn當然。 – alexn 2011-03-05 21:46:15
1
看看在setTimeout功能。你可以這樣做:
function displayLink() {
document.getElementById('link_id').style.display = 'block';
}
setTimeout(displayLink, 10000);
1
完整的示例:
<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>
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);
}
相關問題
- 1. php隱藏下載鏈接
- 2. 下載鏈接隱藏
- 3. 如何隱藏用Javascript下載鏈接
- 4. 如何在Facebook標籤上隱藏鏈接10秒
- 5. 隱藏鏈接
- 6. Popup對話框隱藏10秒後
- 7. 在Android 10秒後隱藏佈局?
- 8. 在JS隱藏下拉菜單中選擇鏈接
- 9. 圖片鏈接隱藏鏈接下面的鏈接
- 10. 隱藏下載鏈接的網絡流量
- 11. 隱藏下載鏈接的替代方法
- 12. PHP下載大文件的隱藏url鏈接的簡歷
- 13. 在按鈕上運行PHP並單擊隱藏下載鏈接
- 14. 用戶共享鏈接後顯示隱藏的下載框
- 15. 隱藏鏈接在html
- 16. jQuery的 - 鏈接隱藏DIV
- 17. 部分鏈接被隱藏
- 18. 隱藏在asp.net鏈接
- 19. PHP顯示/隱藏鏈接
- 20. 非惡意隱藏鏈接
- 21. 隱藏鏈接在jquery datatable
- 22. 無法隱藏鏈接
- 23. 點擊時隱藏鏈接
- 24. 隱藏的特殊鏈接
- 25. 在訪問者分享鏈接後,顯示隱藏的下載框格div
- 26. 如何隱藏js套接字連接?
- 27. 下載鏈接
- 28. js:隱藏div
- 29. 直接鏈接到隱藏的div
- 30. 原型 - 如何延緩調用隱藏()以10秒
瑞安米切爾的答案是正確的,但如果你不保護在服務器端的鏈接,持續10秒爲好,你的下載保護將是幼稚容易旁路。 – 2011-03-05 21:45:03