神奇功能呢?
說說@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/
您可以設置一個HTML exapmple呢?它似乎並不適用於我使用的HTML。謝謝你的幫助! – user1312608 2012-04-04 12:31:39
當然,你去了:http://jsfiddle.net/Ralt/kTbcm/ – 2012-04-04 12:35:07
非常感謝。這非常有幫助。只有一個問題。我想顯示其他內容(例如廣告),並且內容在第1秒後發生更改 - > http://jsfiddle.net/kTbcm/1/,所以我想知道是否可以將在HTML或其他變量的額外內容? – user1312608 2012-04-04 12:48:39