<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv = "content-type" content="text/html;charset=utf-8"/>
<script src="js.js" type="text/javascript" language="javascript">
</script>
</head>
<body>
<form>
<input type = "text" id = "time" />
<input type = "button" value = "启动时钟" onclick = "start()"/>
<input type = "button" value = "关闭时钟" onclick = "stop()"/>
</form>
</body>
</html>
//js.js
var timer;
function start(){
timer = window.setInterval(time,1000);///设置定时器 第一个参数表示要干什么 第二个参数表示时间间隔 返回一个时钟,用于区别其他时钟以及关闭
}
function time(){
var date = new Date();///获得一个Date对象
var datestr = date.toLocaleTimeString();///取得时间 没有年月日
document.getElementById("time").value = datestr;
}
function stop(){
window.clearInterval(timer);///关闭时钟 参数为设置时钟时的返回值
}
另一时钟 用于只执行一次的时钟
//jsdemo.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv = "content-type" content="text/html;charset=utf-8"/>
<script src="js.js" type="text/javascript" language="javascript">
</script>
</head>
<body>
<form>
<input type = "button" value = "启动时钟" onclick = "waithello();"/>
<a href = "javascript:cancel();">取消</a>
</form>
</body>
</html>
//js.js
var time1;
function waithello(){
time1 = window.setTimeout("alert('hello')",3000);///window.setTimeout(f,时间) 第一个参数是要执行的内容 可以是一个方法,第二个是间隔的时间,
//用于将一个操作延迟一定的时间 执行一次
}
function cancel(){
//用于当不想将操作延迟时 可以打断延迟,取消延迟
window.clearTimeout(time1);
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。