Javascript jquery倒计时上的暂停按钮
我有这个jquery倒计时,我需要在点击时放置一个停止按钮,暂停倒计时,感谢您的帮助Javascript jquery倒计时上的暂停按钮,javascript,jquery,Javascript,Jquery,我有这个jquery倒计时,我需要在点击时放置一个停止按钮,暂停倒计时,感谢您的帮助 $('startClock')。单击(函数(){ $('siguiente').fadeOut(); $('#startClock').hide(); $('#count').fadeIn(); var计数器=30; setInterval(函数(){ 计数器--; 如果(计数器>=0){ span=document.getElementById(“计数”); span.innerHTML=计数器; } 如果
$('startClock')。单击(函数(){
$('siguiente').fadeOut();
$('#startClock').hide();
$('#count').fadeIn();
var计数器=30;
setInterval(函数(){
计数器--;
如果(计数器>=0){
span=document.getElementById(“计数”);
span.innerHTML=计数器;
}
如果(计数器==0){
清除间隔(计数器);
$(“#count”).html(“EXCELENTE!
”)
$('siguiente').fadeIn();
$('#startClock').fadeIn().text('REPETIR');
}
}, 1000);
});代码>
30
您需要将间隔存储在变量中,并按如下方式清除它:
var myInterval=setInterval(函数(){},1000);
clearInterval(myInterval)代码>您需要将间隔存储在变量中,并按如下方式清除它:
var myInterval=setInterval(函数(){},1000);
clearInterval(myInterval)代码>var计数器=30
var区间;
$('#startClock')。单击(函数(){
$('siguiente').fadeOut();
$('#startClock').hide();
$('#count').fadeIn();
间隔=设置间隔(函数(){
计数器--;
如果(计数器>=0){
span=document.getElementById(“计数”);
span.innerHTML=计数器;
}
如果(计数器==0){
清除间隔(计数器);
$(“#count”).html(“EXCELENTE!
”)
$('siguiente').fadeIn();
$('#startClock').fadeIn().text('REPETIR');
}
}, 1000);
});
$(“#btnStopCounter”)。单击(函数(){
间隔时间;
});
像这样的?首先,您的计数器需要是全局的,这样您可以在暂停后再次继续
您需要将间隔保存在变量中,以便在停止计数器功能中清除它。var counter=30
var区间;
$('#startClock')。单击(函数(){
$('siguiente').fadeOut();
$('#startClock').hide();
$('#count').fadeIn();
间隔=设置间隔(函数(){
计数器--;
如果(计数器>=0){
span=document.getElementById(“计数”);
span.innerHTML=计数器;
}
如果(计数器==0){
清除间隔(计数器);
$(“#count”).html(“EXCELENTE!
”)
$('siguiente').fadeIn();
$('#startClock').fadeIn().text('REPETIR');
}
}, 1000);
});
$(“#btnStopCounter”)。单击(函数(){
间隔时间;
});
像这样的?首先,您的计数器需要是全局的,这样您可以在暂停后再次继续
您需要将间隔保存在变量中,以便在停止计数器功能中清除它。在这里
timer=setInterval(function(){$(“button”).text(Number($($(“button”).text())+1)},1000;
$(“按钮”)。单击(函数(){
清除间隔(计时器);
});代码>
点击按钮停止倒计时!
0
在这里
timer=setInterval(function(){$(“button”).text(Number($($(“button”).text())+1)},1000;
$(“按钮”)。单击(函数(){
清除间隔(计时器);
});代码>
点击按钮停止倒计时!
0
这可能会起作用
$(document).ready(function(){
var counter = 30;
var running = false
var timer = null;
function setTimer(){
running = true;
timer = setInterval(function() {
counter--;
if (counter >= 0) {
span = document.getElementById("count");
span.innerHTML = counter;
}
if (counter === 0) {
clearInterval(counter);
$('#count').html('<p style="font-size:18px;">EXCELENTE!</p>')
$('#siguiente').fadeIn();
$('#startClock').fadeIn().text('REPETIR');
}
}, 1000);
}
$('#siguiente').on('click', function(){
if(running){
clearInterval(timer)
running = false;
} else {
setTimer()
}
})
$('#startClock').on('click', function(){
setTimer();
});
})
$(文档).ready(函数(){
var计数器=30;
var running=false
var定时器=null;
函数setTimer(){
运行=真;
计时器=设置间隔(函数(){
计数器--;
如果(计数器>=0){
span=document.getElementById(“计数”);
span.innerHTML=计数器;
}
如果(计数器==0){
清除间隔(计数器);
$(“#count”).html(“EXCELENTE!
”)
$('siguiente').fadeIn();
$('#startClock').fadeIn().text('REPETIR');
}
}, 1000);
}
$('siguiente')。在('click',function()上{
如果(正在运行){
清除间隔(计时器)
运行=错误;
}否则{
setTimer()
}
})
$('#startClock')。在('click',function()上{
setTimer();
});
})
这可能会奏效
$(document).ready(function(){
var counter = 30;
var running = false
var timer = null;
function setTimer(){
running = true;
timer = setInterval(function() {
counter--;
if (counter >= 0) {
span = document.getElementById("count");
span.innerHTML = counter;
}
if (counter === 0) {
clearInterval(counter);
$('#count').html('<p style="font-size:18px;">EXCELENTE!</p>')
$('#siguiente').fadeIn();
$('#startClock').fadeIn().text('REPETIR');
}
}, 1000);
}
$('#siguiente').on('click', function(){
if(running){
clearInterval(timer)
running = false;
} else {
setTimer()
}
})
$('#startClock').on('click', function(){
setTimer();
});
})
$(文档).ready(函数(){
var计数器=30;
var running=false
var定时器=null;
函数setTimer(){
运行=真;
计时器=设置间隔(函数(){
计数器--;
如果(计数器>=0){
span=document.getElementById(“计数”);
span.innerHTML=计数器;
}
如果(计数器==0){
清除间隔(计数器);
$(“#count”).html(“EXCELENTE!
”)
$('siguiente').fadeIn();
$('#startClock').fadeIn().text('REPETIR');
}
}, 1000);
}
$('siguiente')。在('click',function()上{
如果(正在运行){
清除间隔(计时器)
运行=错误;
}否则{
setTimer()
}
})
$('#startClock')。在('click',function()上{
setTimer();
});
})
您可以使用一些全局变量来检查开始
或停止
状态
var Paused = false;
var counter = 30;
$('#startClock').click(function() {
$('#siguiente').fadeOut();
var text = (Paused) ? "Start":"Stop";
$('#startClock').text(text);
$('#count').fadeIn();
Count();
});
function Count() {
Paused = !Paused;
setInterval(function() {
if(Paused) {
counter--;
if (counter >= 0) {
span = document.getElementById("count");
span.innerHTML = counter;
}
if (counter === 0) {
clearInterval(counter);
$('#count').html('<p style="font-size:18px;">EXCELENTE!</p>')
$('#siguiente').fadeIn();
$('#startClock').fadeIn().text('REPETIR');
}
}
}, 1000);
}
var暂停=false;
var计数器=30;
$('#startClock')。单击(函数(){
$('siguiente').fadeOut();
变量文本=(暂停)?“开始”:“停止”;
$('#startClock').text(text);
$('#count').fadeIn();
计数();
});
函数计数(){
暂停=!暂停;
setInterval(函数(){
如果(暂停){
计数器--;
如果(计数器>=0){
span=document.getElementById(“计数”);
span.innerHTML=计数器;
}
如果(计数器==0){
清除间隔(计数器);
$(“#count”).html(“EXCELENTE!
”)
$('siguiente').fadeIn();
$('#startClock').fadeIn().text('REPETIR');
}
}
}, 1000);
}
您可以使用一些全局变量来检查开始
或停止
状态
var Paused = false;
var counter = 30;
$('#startClock').click(function() {
$('#siguiente').fadeOut();
var text = (Paused) ? "Start":"Stop";
$('#startClock').text(text);
$('#count').fadeIn();
Count();
});
function Count() {
Paused = !Paused;
setInterval(function() {
if(Paused) {
counter--;
if (counter >= 0) {
span = document.getElementById("count");
span.innerHTML = counter;
}
if (counter === 0) {
clearInterval(counter);
$('#count').html('<p style="font-size:18px;">EXCELENTE!</p>')
$('#siguiente').fadeIn();
$('#startClock').fadeIn().text('REPETIR');
}
}
}, 1000);
}
var暂停=false;
var计数器=30;
$('#startClock')。单击(函数(){
$('siguiente').fadeOut();
变量文本=(暂停)?“开始”:“停止”;
$('#startClock')。文本(text)