gpt4 book ai didi

javascript - PHP/Javascript 倒计时器脚本

转载 作者:行者123 更新时间:2023-12-03 00:44:53 26 4
gpt4 key购买 nike

我有 IRC channel 的统计网页,每 15 分钟更新一次。
我想在与我的服务器 unix 时间链接的网页上显示实时倒计时,从 15:00 分钟到 00:00,然后在 15:00 分钟再次开始。
如果刷新网页,计时器不得刷新回 15:00。

我在互联网上搜索并尝试了很多脚本,但只找到了计算到 future 特定日期的倒计时。
然后找到以下解决方案,但我收到错误“Uncaught SyntaxError: Unexpected token <

如何修复此错误或者是否有其他脚本/方式?
如果有人帮助我,我将非常感激,谢谢。
P.S:如果我犯了任何错误,我很抱歉,因为这是我在 stackoverflow 上的第一篇文章。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
#time {
}
</style>

<?
$currenttime = time();
$mins = date('i', (15 * 60) - ($currenttime % (15 * 60)));
$secs = date('s', (15 * 60) - ($currenttime % (15 * 60)));
$usercount = date('i:s', (15 * 60) - ($currenttime % (15 * 60)));
?>
<script type="text/javascript">
var m = <? echo "$mins"; ?> ;
var s = <? echo "$secs"; ?> ;
window.onload=function() {
fifteenMinutes();
}
function fifteenMinutes(){
s--;
if(s<0) {
s=59;
m--;
}
if(m==-1) {
m=14; #
}
if(m<10) {
mins='0'+m;
}
else {
mins=m;
}
if(s<10) {
secs='0'+s;
}
else {
secs=s;
}
document.getElementById('time').firstChild.nodeValue=mins+':'+secs;
cd=setTimeout('fifteenMinutes()',1000);
}
</script>
</head>
<body>
<div id="time"> <? echo "$usercount"; ?>
</body>
</html>

最佳答案

这是从 15 分钟开始的简单倒计时。

function tick(time) {
setTimeout(() => {
const newTime = new Date();
const diff = Math.floor((newTime.getTime() - time.getTime()) / 1000);
const timeFrom15 = (15 * 60) - diff;
const secs = timeFrom15 % 60;
const mins = ((timeFrom15 - secs) / 60);
document.getElementById('time').innerText = `${mins}:${secs < 10 ? '0' : ''}${secs}`;
if (secs >= 0) {
tick(time);
} else {
document.getElementById('time').innerText = '0:00';
}
}, 1000);
}

tick(new Date());
<div id="time"></div>

关于javascript - PHP/Javascript 倒计时器脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53291906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com