神奇功能呢?
说说@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