如果一运行这段代码就会打印出时实变化的时间
比如:已运行0天0小时0分钟15秒
每过一秒会自动刷新打印结果。
你要的html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello world</title>
</head>
<body>
<div id="time"></div>
<input type="button" id="start" value="开始">
<input type="button" id="stop" value="暂停">
<input type="button" id="reset" value="重置">
<script>
var timer = (function () {
var start, temp;
function run() {
start = start || +new Date();
if (temp) {
start += (new Date() - temp);
temp = 0;
}
}
function stop() {
if (start && !temp) {
temp = +new Date();
}
}
function reset() {
start = 0;
temp = 0;
}
function getTime() {
return start ? (temp ? temp : new Date()) - start : 0;
}
function getText() {
var now = getTime() / 1000 >> 0;
var h, m, s;
s = now % 60;
now = now / 60 >> 0;
m = now % 60;
now = now / 60 >> 0;
h = now % 24;
now = now / 24 >> 0;
return now + ' 天 ' + h + ' 时 ' + m + ' 分 ' + s + ' 秒 ';
}
return {
start: run,
stop: stop,
reset: reset,
getTime: getTime,
getText: getText
}
})();
function getId(id) {
return document.getElementById(id);
}
getId('start').onclick = timer.start;
getId('stop').onclick = timer.stop;
getId('reset').onclick = timer.reset;
setInterval(function () {
getId('time').innerText = timer.getText();
}, 100);
</script>
</body>
</html>
console的写法。
import sys
from datetime import datetime
from time import sleep
start_time = datetime.now()
while True:
sleep(1)
delta = datetime.now() - start_time
message = 'Program running time: {}'.format(str(delta).split('.')[0])
sys.stdout.write('\r' + message)
sys.stdout.flush()
4 回答4.4k 阅读✓ 已解决
4 回答3.8k 阅读✓ 已解决
3 回答1.4k 阅读✓ 已解决
3 回答2.1k 阅读✓ 已解决
1 回答4.4k 阅读✓ 已解决
2 回答2.5k 阅读✓ 已解决
3 回答1.1k 阅读✓ 已解决
print函数结尾默认是换行,改成
\r
代表回车print('xxx', end='\r')