求一个JS倒计时的函数
发布网友
发布时间:2022-05-09 18:49
我来回答
共1个回答
热心网友
时间:2023-10-13 11:09
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="keywords" content="白菜编辑部">
<title>白菜编辑部</title>
<style type="text/css">
</style>
<script type="text/javascript">
var display = function (obj)
{
var reg = /^(\-?)(\d)$/;
var n = new Date;
var str = n.getFullYear () + "/" + (n.getMonth () + 1) + "/" + n.getDate () + " " + obj.value;
var end = new Date (str);
var now = document.getElementById ('now').innerHTML = n.toLocaleTimeString ();
var h = Math.floor ((end.getTime () - n.getTime ()) / 1000 / 60 / 60) + "";
h = reg.test (h) ? h.replace (reg, "$10$2") : h;
var m = Math.floor ((end.getTime () - n.getTime ()) / 1000 / 60 % 60) + "";
m = reg.test (m) ? m.replace (reg, "$10$2") : m;
var s = Math.floor ((end.getTime () - n.getTime ()) / 1000 % 60) + "";
s = reg.test (s) ? s.replace (reg, "$10$2") : s;
document.getElementById ('other').innerHTML = h + ":" + m + ":" + s;
clearTimeout (this.timer);
this.timer = setTimeout (function ()
{
display (obj);
}, 1000);
}
</script>
</head>
<body>
比如我给个时间:
<input type="text" id="txt" onblur="display(this);" /> 现在是
<span id="now"></span> 就自动显示 ==> 剩余时间:
<span id="other"></span> 并且1秒1秒的调减
</body>
</html>
追问在 html里面直接调用怎么办? 不要用 ,”要计算的时间“是打开页面时直接就调用来的