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)的更多相关文章

  1. JavaScript之关闭轮询定时器(setTimeout/clearTimeout|setInterval/clearInterval)小结

    已知: 1.1 开启Timeout程序: scope.setTimeout("functionName()" | functionHandle, timeValue) 返回值:ti ...

  2. javascript定时器:setTimeout与setInterval

    概述: setTimeout:在指定的延迟时间之后调用一个函数或者执行一个代码片段,只执行一次: setInterval:周期性地调用一个函数(function)或者执行一段代码,重复执行: 语法格式 ...

  3. 关闭定时器(setTimeout/clearTimeout|setInterval/clearInterval)

    1.1 开启Timeout程序: scope.setTimeout("functionName()" | functionHandle, timeValue) 返回值:timerI ...

  4. JavaScript定时器详解

    假设有以下场景 setTimeout(function timeoutHandler(){ /*Some timeout handle code that runs for 6ms*/ }, 10); ...

  5. Javascript定时器(二)——setTimeout与setInterval

    一.解释说明 1.概述 setTimeout:在指定的延迟时间之后调用一个函数或者执行一个代码片段 setInterval:周期性地调用一个函数(function)或者执行一段代码. 2.语法 set ...

  6. Javascript的setTimeOut()和setInterval()的定时器用法

    Javascript用来处理延时和定时任务的setTimeOut和setInterval函数应用非常广泛,它们都用来处理延时和定时任务,比如打开网页一段时间后弹出一个登录框,页面每隔一段时间发送异步请 ...

  7. Javascript 笔记与总结(2-13)定时器 setTimeout 和 setInterval

    定时器可以让 js 效果每隔几秒钟执行一次或者 n 秒之后执行某一个效果.定时器不属于 javascript,是 window 对象提供的功能. setTimeout 用法: window.setTi ...

  8. javascript中window与document对象、setInterval与setTimeout定时器的用法与区别

    一.写在前面 本人前端菜鸟一枚,学习前端不久,学习过程中有很多概念.定义在使用时容易混淆,在此给向我一样刚踏入前端之门的童鞋们归纳一下.今天给大家分享一下js中window与document对象.se ...

  9. javascript 定时器 timer setTimeout setInterval (js for循环如何等待几秒再循环)

    实现一个打点计时器,要求1.从 start 到 end(包含 start 和 end),每隔 100 毫秒 console.log 一个数字,每次数字增幅为 12.返回的对象中需要包含一个 cance ...

随机推荐

  1. input="file" 浏览时只显示指定excel文件,筛选特定文件类型

    <p>显示 .xls, .xlsx, .csv 文件...</p> <input type="file" accept=".csv, app ...

  2. Java+selenium 如何定位下拉框select

    场景:需要进行下拉选择定位元素.   一.select菜单       select也是比较常见的,selenium封装了以下方法, 创建select WebElement selector = dr ...

  3. shell 变量的数值计算实践 expr

    expr(evaluate(求值)expressions(表达式))命令既可以用于整数运算,也可以用于相关字符串长度.匹配等的运算处理 expr 用做计算 必须有一个空格才会计算 [root@salt ...

  4. centos who命令 查看当前登录系统用户信息

    who 显示当前登录系统的用户,但w显示的更为详细 默认输出 [root@mysql ~]# who //用户名.登录终端.登录时间 root pts/ -- : (192.168.0.110) -a ...

  5. 前端 HTML body标签相关内容 常用标签 定义列表<dl>

    定义列表<dl> 定义列表的作用非常大. <dl>英文单词:definition list,没有属性.dl的子元素只能是dt和dd. <dt>:definition ...

  6. centos下搭建sockets5代理

    #安装依赖及ss5 yum -y install gcc openldap-devel pam-devel openssl-devel wget https://nchc.dl.sourceforge ...

  7. RestFramework——API基本实现及dispatch基本源码剖析

    基于Django实现 在使用RestFramework之前我们先用Django自己实现以下API. API完全可以有我们基于Django自己开发,原理是给出一个接口(URL),前端向URL发送请求以获 ...

  8. 自定义安装visual studio 2010开发asp.net

    VS2010的安装对于VS的安装大家肯定都熟悉,不过我在很多地方看到的是大家讲VS的全部组件都安装了,不但浪费磁盘空间,还降低了系统性能,除此之外,还有人安装了VS之后不知道顺手把MSDN安装上,害得 ...

  9. 华为核心交换机绑定IP+MAC+端口案例

    1         案例背景 某网络改造项目,核心交换机为华为S5700,接入交换机为不同型号交换机,如下模拟拓扑,客户端接入交换机1通过Access模式与核心交换机连接,该交换机下只有一个Vlan2 ...

  10. DLNg第三周:序列模型和注意力机制

    1.基础模型 将法语翻译为英语,分为编码和解码阶段,将一个序列变为另一个序列.即序列对序列模型. 从图中识别出物体的状态,将图片转换为文字. 先使用CNN处理图片,再使用RNN将其转换为语言描述. 2 ...