JavaScript Javascript在几分钟和几秒内倒计时

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript Javascript在几分钟和几秒内倒计时相关的知识,希望对你有一定的参考价值。

<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
// set minutes
var mins = 5;

// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;
function countdown() {
	setTimeout('Decrement()',1000);
}
function Decrement() {
	if (document.getElementById) {
		minutes = document.getElementById("minutes");
		seconds = document.getElementById("seconds");
		// if less than a minute remaining
		if (seconds < 59) {
			seconds.value = secs;
		} else {
			minutes.value = getminutes();
			seconds.value = getseconds();
		}
		secs--;
		setTimeout('Decrement()',1000);
	}
}
function getminutes() {
	// minutes is seconds divided by 60, rounded down
	mins = Math.floor(secs / 60);
	return mins;
}
function getseconds() {
	// take mins remaining (as seconds) away from total seconds remaining
	return secs-Math.round(mins *60);
}
</script>
</head>
<body>

<div id="timer">
	This is only valid for the next <input id="minutes" type="text" style="width: 14px; border: none; background-color:none; font-size: 16px; font-weight: bold;"> minutes and <input id="seconds" type="text" style="width: 26px; border: none; background-color:none; font-size: 16px; font-weight: bold;"> seconds.
</div>
<script>
countdown();
</script>

以上是关于JavaScript Javascript在几分钟和几秒内倒计时的主要内容,如果未能解决你的问题,请参考以下文章

防止iOS在几分钟后杀死App

什么是JavaScript?1分钟了解JavaScript开发!

为啥我的 websocket 在几分钟后关闭?

在 JavaScript 中将 Dialogfow 持续时间系统实体从分钟转换为秒

为啥 Heroku 服务器在几分钟后关闭?

#yyds干货盘点#--30分钟入门JavaScript