目前我有这个代码
setTimeout(function() { $('#hideMsg').fadeOut('fast'); },2000);
是否有可能将倒数计时器放置2秒钟?
喜欢
<div id="hideMsg"> The entry you posted not valid. This Box will Close In 2 Seconds </div>
“此框将关闭2秒”将自动更改为2秒1秒
解决方法
使用
setInterval而不是
setTimeout,然后结合
clearInterval
var sec = 2
var timer = setInterval(function() {
$('#hideMsg span').text(sec--);
if (sec == -1) {
$('#hideMsg').fadeOut('fast');
clearInterval(timer);
}
},1000);
HTML
<div id="hideMsg"> The entry you posted not valid. This Box will Close In <span>2</span> Seconds </div>
crazy demo
做得更好
var sec = $('#hideMsg span').text() || 0;
var timer = setInterval(function() {
$('#hideMsg span').text(--sec);
if (sec == 0) {
$('#hideMsg').fadeOut('fast');
clearInterval(timer);
}
},1000);
所以时间将取决于< span>内部的内容。例如,< span> 2< / span>是2秒,< span> 5< / span>为5秒,< span> 60< / span>是1分钟。
another crazy demo