admin 管理员组文章数量: 1086019
what i want to achieve is when you click this href <a href="javascript:void(0)" class="pauser">pause/continue</a>
the timer pauses and continues when pressed again.
<script>
$(document).ready(function () {
var counter = 10;
var id = setInterval(function() {
counter--;
if(counter > 0) {
var msg = 'You can continue ' + counter + ' seconds';
$('#notice').text(msg);
} else {
$('#notice').hide();
$('#download').show();
clearInterval(id);
}
}, 1000);
});
</script>
My HTML in relevance to the jQuery is here if you need.
<a href="" id="download" class="button" style="display: none;font-size:30px;">Continue !!</a><p id="notice">
You can continue in 10 seconds</p>
what i want to achieve is when you click this href <a href="javascript:void(0)" class="pauser">pause/continue</a>
the timer pauses and continues when pressed again.
<script>
$(document).ready(function () {
var counter = 10;
var id = setInterval(function() {
counter--;
if(counter > 0) {
var msg = 'You can continue ' + counter + ' seconds';
$('#notice').text(msg);
} else {
$('#notice').hide();
$('#download').show();
clearInterval(id);
}
}, 1000);
});
</script>
My HTML in relevance to the jQuery is here if you need.
<a href="http://myurl." id="download" class="button" style="display: none;font-size:30px;">Continue !!</a><p id="notice">
You can continue in 10 seconds</p>
Share
Improve this question
edited Nov 16, 2011 at 21:53
Manse
38.1k11 gold badges86 silver badges111 bronze badges
asked Nov 16, 2011 at 21:46
Yusaf KhaliqYusaf Khaliq
3,39311 gold badges45 silver badges82 bronze badges
0
1 Answer
Reset to default 7Well, I would just have your pause event set a boolean, and then check that boolean before you decrement your counter:
<a href="javascript:setPause();" class="pauser">pause/continue</a>
and
var ispaused=false;
function setPause(){
ispaused=!ispaused;
}
and
$(document).ready(function () {
var counter = 10;
var id = setInterval(function() {
if(!ispaused){ ////the only new line I added from your example above
counter--;
if(counter > 0) {
var msg = 'You can continue ' + counter + ' seconds';
$('#notice').text(msg);
} else {
$('#notice').hide();
$('#download').show();
clearInterval(id);
}
}
}, 1000);
});
That should do it, right?
本文标签: javascriptpause and continue using setIntervalStack Overflow
版权声明:本文标题:javascript - pause and continue using setInterval - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744036453a2522452.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论