2012-04-04 79 views
3

我想創建一個鏈接,在顯示我的論壇上的直接鏈接之前顯示內容。jQuery;點擊以顯示html內容和倒計時,然後附加內容

  1. 顯示鏈接,下載附件
  2. 點擊後,顯示HTML內容下面
  3. 當倒計時結束時,它顯示了一個直接鏈路5秒倒計時。

我曾嘗試下面的代碼:

$("button").click(function() { 
    $(function() { 
    var count = 10; 
    countdown = setInterval(function() { 
     $("p.countdown").html(count + " seconds remaining!").show("slow"); 

     if (count == 0) { 
     $("p.new").show("slow"); 
     } 

     count--; 
    }, 1000); 
    }); 
}); 

回答

2

神奇功能呢?

說說@Bradley Foster的回答,多次致電setTimeout是不可靠的。如果您的瀏覽器滯後,setTimeout將會停止,所以有四個不同點,您不確定訂單是否合適。嵌套0​​,因爲我顯示更好。

$('#button').click(function() { 
    var seconds = 5, // Declare some variables for reuse 
     el = $('#some_link') 
    el.text(seconds) // Put it a five! 
    // Name your function so that you can call it later 
    setTimeout(function countdown() { 
     // Your countdown is already at 5, so decrement it 
     // Remember that you've already waited for 1000ms before reaching this line the first time 
     seconds-- 
     el.text(seconds) // Set the new time left 
     // If the countdown is not over, recall this function after 1000ms 
     if (seconds > 0) { 
      setTimeout(countdown, 1000) 
     } 
     // If it is over, display the link 
     // Note that js will stop there and not try to call itself another time as it would with setInterval() 
     else { 
      el.html('<a href="link">Download</a>') 
     } 
    }, 1000) 
}) 

順便說一下,在你的問題,當你做$(function() {...,你實際上在做$(document).ready(function() {...。我想這是不是你想要的:)

的jsfiddle這裏:http://jsfiddle.net/Ralt/kTbcm/

+0

您可以設置一個HTML exapmple呢?它似乎並不適用於我使用的HTML。謝謝你的幫助! – user1312608 2012-04-04 12:31:39

+0

當然,你去了:http://jsfiddle.net/Ralt/kTbcm/ – 2012-04-04 12:35:07

+0

非常感謝。這非常有幫助。只有一個問題。我想顯示其他內容(例如廣告),並且內容在第1秒後發生更改 - > http://jsfiddle.net/kTbcm/1/,所以我想知道是否可以將在HTML或其他變量的額外內容? – user1312608 2012-04-04 12:48:39

0
function countdownfunction() { 
    $('#some_link').innerHTML = "5"; 
    $('#some_link').innerHTML = setTimeout("4",1000); 
    $('#some_link').innerHTML = setTimeout("3",1000); 
    $('#some_link').innerHTML = setTimeout("2",1000); 
    $('#some_link').innerHTML = setTimeout("1",1000); 

    $('#some_link').innerHTML = '<a href="newlink.php">download now</a>; 
}; 

$('#some_link').click(countdownfunction()); 
1

You could do something like this:

HTML:

<button>Download</button> 

<p class="countdown" /> 
<p class="link"> 
    <a href="www.stackoverflow.com">StackOverflow</a> 
</p>​ 

CSS:

p { display: none; padding: 50px; border: solid 1px black; } 
p.countdown { color: red; font-size: 24px; }​ 

的jQuery:

var $countdown = $("p.countdown"), 
    $link = $("p.link"), 
    $button = $("button"); 

$button.click(function() { 

    $countdown.hide(0); 
    $link.hide(0);   

    var count = 10, 
     countdown = setInterval(function() { 

      $countdown.html(count + " seconds remaining!").show("slow"); 

      if (count == 0) { 

       $countdown.hide(0); 
       $link.show("slow"); 
       clearInterval(countdown); 

      } 

      count--; 

     }, 1000); 

});​ 
+0

-1使用'setInterval'。這不可靠。看到這個優秀的答案來檢查爲什麼:http://stackoverflow.com/a/731625/851498 – 2012-04-04 12:40:50

+0

@FlorianMargaine - 我很抱歉,但你讀過自己的鏈接?他在他的結論中清楚地表示:「我會考慮我希望儘可能平滑的一次性動畫間隔,而鏈接超時對於正在進行的動畫更加客氣,這些動畫會在頁面加載時一直髮生。「OP的場景是一次性動畫,應該儘可能平滑,並且setInterval符合賬單。 – 2012-04-04 12:43:41

+0

@FlorianMargaine - 事實上,我會進一步聲明,我總是更喜歡'setInterval()'到'setTimeout() 。試圖在JavaScript中保持時間是不可靠的,並且每個瀏覽器都不相同。 – 2012-04-04 12:47:29