var k = 0,
m = 0,
s = 59,
c = 1;
//var session=false;
var arr;
var tick;
function breaktime() {
clearInterval(tick);
k = parseInt(document.getElementById('bk2').innerHTML);
m = k;
s = 59;
if (c < 0)
c = 1;
c--;
if (c >= 0) {
tick = setInterval(function() {
if (m >= 0) { //k--;
//m=k;
if (s >= 10 && m >= 0) {
document.getElementById('clock').innerHTML = m + ":" + s;
s--;
} else if (s >= 0 && m >= 0) {
document.getElementById('clock').innerHTML = m + ":" + "0" + s;
s--;
} else //if(s<=0)
{
m--;
s = 59;
}
//else
//document.getElementById('clock').innerHTML=m+":"+"0"+s;
} else {
document.getElementById('clock').innerHTML = "00:00";
clock();
}
}, 1000);
}
}
document.onclick =
function clock() {
clearInterval(tick);
k = (document.getElementById('clock').innerHTML).toString();
arr = k.split(":");
m = parseInt(arr[0]);
s = parseInt(arr[1]);
if (s >= 10)
document.getElementById('clock').innerHTML = m + ":" + s;
else
document.getElementById('clock').innerHTML = m + ":" + "0" + s;
if (c < 0)
c = 1;
c--;
if (c >= 0) {
tick = setInterval(function() {
if (m >= 0) { //k--;
//m=k;
if (s >= 10 && m >= 0) {
document.getElementById('clock').innerHTML = m + ":" + s;
s--;
} else if (s >= 0 && m >= 0) {
document.getElementById('clock').innerHTML = m + ":" + "0" + s;
s--;
} else //if(s<=0)
{
m--;
s = 59;
}
//else
//document.getElementById('clock').innerHTML=m+":"+"0"+s;
} else {
document.getElementById('clock').innerHTML = "00:00";
breaktime();
}
}, 1000);
}
};
我希望第一个倒数计时器从“clock”div元素中预先设定的分钟开始计数,然后在它变为零之后,则开始倒数计时器倒计时器,它将预先存储的值存储在“bk2”div元素中。
我面临的问题是我的倒数计时器在“时钟”变为“00:00”后停止,我希望它从“bk2”中的值重新启动,但我无法在breaktime功能和时钟功能之间切换。
答案 0 :(得分:0)
document.onclick=
function clock(){
console.log(clock);//works
console.log(document.onclick);//works
};
console.log(clock);//WORKS NOT
console.log(document.onclick);//works
您的时钟功能是一个名为匿名功能的 。它使得在匿名函数中进行递归成为可能。要在外面访问它,您必须使用
document.onclick();
但那风格很糟糕。或者你这样做:
clock = document.onclick=function(){}
将定义函数外部的时钟,使
clock();
...可能