JavaScript--定时器setTimeout()、clearTimeout(var param)和setInterval()、clearInterval(var param)
1.setTimeout()、clearTimeout(var param)
- setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式,只调用一次
- clearTimeout() 方法可取消由 setTimeout() 方法设置的定时操作,参数必须是由 setTimeout() 返回的 ID 值
<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
<style>
#mytime {
background: #bbb;
color: #fff;
display: block;
} .wrapper {
text-align: center;
width: 60%;
margin: 250px auto;
}
</style>
</head> <body> <div class="wrapper">
<h1 id=mytime>显示时间</h1>
<button id="stop" name="button" onclick="stop()">stop</button>
<button id="start" name="button" onclick="start()">start</button>
<button id="reset" name="button" onclick="reset()">reset</button>
</div>
</body>
<script type="text/javascript">
var h = m = s = ms = 0; //定义时,分,秒,毫秒并初始化为0;
var time = 0; function timer() { //定义计时函数
ms = ms + 50; //毫秒
if(ms >= 1000) {
ms = 0;
s = s + 1; //秒
}
if(s >= 60) {
s = 0;
m = m + 1; //分钟
}
if(m >= 60) {
m = 0;
h = h + 1; //小时
}
str = toDub(h) + "时" + toDub(m) + "分" + toDub(s) + "秒" + toDubms(ms) + "毫秒";
mytime = document.getElementById('mytime');
mytime.innerHTML = str; time = setTimeout(timer, 50);
} function reset() { //重置
clearInterval(time);
h = m = s = ms = 0;
document.getElementById('mytime').innerHTML = "00时00分00秒0000毫秒";
document.getElementById("start").removeAttribute("disabled");
document.getElementById("stop").setAttribute("disabled", true);
} function start() { //开始
time =setTimeout(timer,50);
document.getElementById("start").setAttribute("disabled", true);
document.getElementById("stop").removeAttribute("disabled");
} function stop() { //暂停
clearTimeout(time);
document.getElementById("start").removeAttribute("disabled");
document.getElementById("stop").setAttribute("disabled", true);
} function toDub(n) { //补0操作
if(n < 10) {
return "0" + n;
} else {
return "" + n;
}
} function toDubms(n) { //给毫秒补0操作
if(n < 10) {
return "00" + n;
} else {
return "0" + n;
} }
</script> </html>
2.setInterval()、clearInterval(var param)
- setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式,循环调用
- clearInterval(var param) 方法可取消由 setInterval() 函数设定的定时执行操作,参数必须是由 setInterval() 返回的 ID 值
<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
<style>
#mytime {
background: #bbb;
color: #fff;
display: block;
} .wrapper {
text-align: center;
width: 60%;
margin: 250px auto;
}
</style>
</head> <body> <div class="wrapper">
<h1 id=mytime>显示时间</h1>
<button id="stop" name="button" onclick="stop()">stop</button>
<button id="start" name="button" onclick="start()">start</button>
<button id="reset" name="button" onclick="reset()">reset</button>
</div>
</body>
<script type="text/javascript">
var h = m = s = ms = 0; //定义时,分,秒,毫秒并初始化为0;
var time = 0; function timer() { //定义计时函数
ms = ms + 50; //毫秒
if(ms >= 1000) {
ms = 0;
s = s + 1; //秒
}
if(s >= 60) {
s = 0;
m = m + 1; //分钟
}
if(m >= 60) {
m = 0;
h = h + 1; //小时
}
str = toDub(h) + "时" + toDub(m) + "分" + toDub(s) + "秒" + toDubms(ms) + "毫秒";
mytime = document.getElementById('mytime');
mytime.innerHTML = str;
} function reset() { //重置
clearInterval(time);
h = m = s = ms = 0;
document.getElementById('mytime').innerHTML = "00时00分00秒0000毫秒";
document.getElementById("start").removeAttribute("disabled");
document.getElementById("stop").setAttribute("disabled", true);
} function start() { //开始
time = setInterval(timer, 50);
document.getElementById("start").setAttribute("disabled", true);
document.getElementById("stop").removeAttribute("disabled");
} function stop() { //暂停
clearInterval(time);
document.getElementById("start").removeAttribute("disabled");
document.getElementById("stop").setAttribute("disabled", true);
} function toDub(n) { //补0操作
if(n < 10) {
return "0" + n;
} else {
return "" + n;
}
} function toDubms(n) { //给毫秒补0操作
if(n < 10) {
return "00" + n;
} else {
return "0" + n;
} }
</script> </html>
JavaScript--定时器setTimeout()、clearTimeout(var param)和setInterval()、clearInterval(var param)的更多相关文章
- JavaScript之关闭轮询定时器(setTimeout/clearTimeout|setInterval/clearInterval)小结
已知: 1.1 开启Timeout程序: scope.setTimeout("functionName()" | functionHandle, timeValue) 返回值:ti ...
- javascript定时器:setTimeout与setInterval
概述: setTimeout:在指定的延迟时间之后调用一个函数或者执行一个代码片段,只执行一次: setInterval:周期性地调用一个函数(function)或者执行一段代码,重复执行: 语法格式 ...
- 关闭定时器(setTimeout/clearTimeout|setInterval/clearInterval)
1.1 开启Timeout程序: scope.setTimeout("functionName()" | functionHandle, timeValue) 返回值:timerI ...
- JavaScript定时器详解
假设有以下场景 setTimeout(function timeoutHandler(){ /*Some timeout handle code that runs for 6ms*/ }, 10); ...
- Javascript定时器(二)——setTimeout与setInterval
一.解释说明 1.概述 setTimeout:在指定的延迟时间之后调用一个函数或者执行一个代码片段 setInterval:周期性地调用一个函数(function)或者执行一段代码. 2.语法 set ...
- Javascript的setTimeOut()和setInterval()的定时器用法
Javascript用来处理延时和定时任务的setTimeOut和setInterval函数应用非常广泛,它们都用来处理延时和定时任务,比如打开网页一段时间后弹出一个登录框,页面每隔一段时间发送异步请 ...
- Javascript 笔记与总结(2-13)定时器 setTimeout 和 setInterval
定时器可以让 js 效果每隔几秒钟执行一次或者 n 秒之后执行某一个效果.定时器不属于 javascript,是 window 对象提供的功能. setTimeout 用法: window.setTi ...
- javascript中window与document对象、setInterval与setTimeout定时器的用法与区别
一.写在前面 本人前端菜鸟一枚,学习前端不久,学习过程中有很多概念.定义在使用时容易混淆,在此给向我一样刚踏入前端之门的童鞋们归纳一下.今天给大家分享一下js中window与document对象.se ...
- javascript 定时器 timer setTimeout setInterval (js for循环如何等待几秒再循环)
实现一个打点计时器,要求1.从 start 到 end(包含 start 和 end),每隔 100 毫秒 console.log 一个数字,每次数字增幅为 12.返回的对象中需要包含一个 cance ...
随机推荐
- 《linux 进程管理》- ps/top/kill/nice
一:进程简述 二:ps 查看进程 语法 ps * -A 列出所有进程,和 -e 同等效果 * -a 列出不和本终端有关系的所有进程 * -w 显示加宽,可以显示较多信息 * -u 显示有效使用者相关的 ...
- ubuntu14.04下开启ssh服务
1. 安装 sudo apt-get update sudo apt-get install openssh-server 2.开启服务 查看查看ssh服务是否启动 打开"终端窗口" ...
- 去除字符串中的html代码
public static String Html2Text(String inputString) { String htmlStr = inputString; // 含html标签的字符串 St ...
- 深入理解为什么应该使用transform来替代top
话说,这个问题我们得从浏览器得渲染机制说起: 我们先来理解一下 重绘(Repainit)和 回流(Reflow): 重绘:当节点需要更改外观而不会影响布局得,比如改变 color 就称为重绘: 回流: ...
- C#-1-2-C#基础
1-注释符 1).单行注释符:// 2).多行注释符:/**/ 3).文档注释符:// 2-常用快捷键 3-变量类型 4-转义字符 5-语句 1.将相应内容打印到控制台:Console.WriteLi ...
- docker+mysql基本搭建过程
查询镜像 [root@bms-e4e3 ~]# docker search mysql INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.i ...
- logging日志模块的使用
logging日志模块的使用 logging模块中有5个日志级别: debug 10 info 20 warning 30 error 40 critical 50 通常使用日志模块,是用字典进行配置 ...
- 一份C++学习资源,咬牙切齿地好用呀
多年以后,你已经是一名技术总监,有一个美丽的妻子,两个孩子:你已经拥有了现在的你想都不敢想的一切:那时,你也一定会忘记,今天这篇教程,如同一颗石子,铺就过你前进的路. 下面是我们的老师根据现有资源整理 ...
- [Java in NetBeans] Lesson 04. Class / Objects
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Class: Blueprint for an object. (e.g. dog is a class) Object: cust ...
- 20165321 实验一Java开发环境的熟悉-2