JS之自动更新的时间代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS之自动更新的时间代码</title>
</head>
<body>
<script>
var t;
var flag=true;
function stop(){
clearInterval(t);
flag=false;
console.log('停止的定时器是:'+t);
}
function jixu(){
if(flag!=true){
t = setInterval(clock, 1000);
}
flag=true;
console.log('定时器是:'+t);
}
function check(x){
if(x<10){
return '0'+x;
}
return x;
}
function clock(){
var time = new Date();
var year = time.getFullYear();
var month = time.getMonth()+1;
var day = time.getDay();
var hour = check(time.getHours());
var minute = check(time.getMinutes());
var second = check(time.getSeconds());
document.getElementById('c').innerHTML = year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second;
}
clock();
t = setInterval(clock, 1000);
</script>
</body>
</html>