如何使用 jquery 创建一个以登录弹出窗口结束的 5 秒倒计时计时器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3089475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Can I create A 5 second Countdown timer with jquery that ends with a login popup?
提问by Tapha
How would i create a jquery timer that starts when a link is 'mouse-overed', Displays a 1,2,3, 4 and 5, one after the other. Then on 5 pops up a login box?
我将如何创建一个 jquery 计时器,当链接被“鼠标悬停”时启动,一个接一个地显示 1、2、3、4 和 5。然后在5上弹出一个登录框?
Cheers.
干杯。
回答by VoteyDisciple
How about:
怎么样:
var counter = 0;
var interval = setInterval(function() {
counter++;
// Display 'counter' wherever you want to display it.
if (counter == 5) {
// Display a login box
clearInterval(interval);
}
}, 1000);
回答by Ifti Mahmud
This is exactly the code that worked for me:
这正是对我有用的代码:
<p>You'll be automatically redirected in <span id="count">10</span> seconds...</p>
<script type="text/javascript">
window.onload = function(){
(function(){
var counter = 10;
setInterval(function() {
counter--;
if (counter >= 0) {
span = document.getElementById("count");
span.innerHTML = counter;
}
// Display 'counter' wherever you want to display it.
if (counter === 0) {
// alert('this is where it happens');
clearInterval(counter);
}
}, 1000);
})();
}
</script>
<meta http-equiv="refresh" content="10;url=http://www.example.com" />
Hope it helps ;)
希望能帮助到你 ;)
回答by Brynner Ferreira
http://jsfiddle.net/brynner/Lhm1ydvs/
http://jsfiddle.net/brynner/Lhm1ydvs/
HTML
HTML
<span class="c" id="5"></span>
JS
JS
function c(){
var n=$('.c').attr('id');
var c=n;
$('.c').text(c);
setInterval(function(){
c--;
if(c>=0){
$('.c').text(c);
}
if(c==0){
$('.c').text(n);
}
},1000);
}
// Start
c();
// Loop
setInterval(function(){
c();
},5000);