<html>
<head>
<meta charset=UTF-8>
<title>recursion</title>
<style type="text/css">
</style>
<script type="text/javascript">
onload = function ()
{
setInterval (function ()
{
var date = new Date ()
date.setHours (date.getHours () + 1)
var m = date.getMonth () + 1
m = m < 10 ? "0" + m : m
var d = date.getDate ()
d = d < 10 ? "0" + d : d
var h = date.getHours ()
h = h < 10 ? "0" + h : h
var mm = date.getMinutes ()
mm = mm < 10 ? "0" + mm : mm
txt.value = m + "-" + d + "-" + h + "-" + mm
}, 1000)
}
</script>
</head>
<body>
<input type="text" id="txt" />
</body>
</html>
1 小时数得保证是正整数,开始时间保证是合法的时间格式
2 代码
function count() {var hours = document.getElementById("xiaoshishu").value
var startDate = document.getElementById("kaishi").value
hours = parseInt(hours)
startDate = new Date(startDate)
var newDate = new Date(startDate.setHours(startDate.getHours() + hours))
document.getElementById("jieshu").value = `${newDate.getFullYear()}-${newDate.getMonth() + 1}-${newDate.getDate()} ${newDate.getHours()}:${newDate.getMinutes()}`
}
count()
js代码:fun.jsvar timmerID = null
function time() {
var time = document.getElementById("now")
var left = document.getElementById("left")
var t = time.value.split(":")
var hour = parseInt(t[0])
var min = parseInt(t[1])
min=min+1
if(min==60){
hour=hour+1
min=0
if(hour==24){
hour=0
}
}
time.value=hour+":"+min
left.value = left.value-1
if(left.value==0){
mystop()
alert("时间已到!")
}
}
function mystart() {
var left = document.getElementById("left")
left.value="40"
timmerID = window.setInterval("time()", 1000*60)
}
function mystop() {
window.clearInterval(timmerID)
}
html页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>time.html</title>
<script type="text/javascript" src="fun.js"></script>
</head>
<body>
时间起点:<input type="text" name="now" id="now" value="0:0"/><input type="button" value="开始" onclick="mystart()"><br>
剩余时间:<input type="text" name="left" id="left" value="40" disabled="disabled"/>分钟<br>
</body>
</html>
补充:这个javascript放在一个文件叫fun.js,在html页面中要引用此文件,如:<script type="text/javascript" src="fun.js"></script>,注意不要把路径弄错了,src后面的路径按照实际的来,如果两个文件在一个目录下,就是如上这样引用,当然也可以直接把全部的js内容放在html文件的头部,如:<script type="text/javascript">
<!--javascript代码-->
</script>