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. oracle 监听报错the information provided for this listener is currently in use by other software on this computer

    use another port number: the information provided for this listener is currently in use by other sof ...

  2. Qt获取QObject对应的类名并把它转为真实类型(使用obj->metaObject()->className() )

    QObject是有窗口类的父类,比如QWidget,QLabel,QPushButton等都直接或间接继承自QObject类.如果把某个窗口中的所有控件都装到一个QList<QObject*&g ...

  3. PyChram快捷键

  4. 实践-通过matlab操作sqlite数据库

    1.下载sqlite-jdbc连接数据库地址 https://bitbucket.org/xerial/sqlite-jdbc/downloads/ 2.将该jar包的绝对路径写入matlab的cla ...

  5. oracle常见的执行计划

    访问表的执行计划: 全表扫描:TABLE ACCESS FULL ROWID扫描:TABLE ACCESS BY USER ROWID (ROWID来源于用户在where条件中的指定)或 TABLE ...

  6. fdisk vs df

    fdisk工具是分区工具:df是用来查看文件系统(分区)的使用情况的! 当用来查看分区信息时,较为相似: fdisk侧重于显示分区表的信息: df侧重于显示当前系统中所有文件系统的信息: 常用用法: ...

  7. yum方式安装lnmp

    1.安装mysql>>>yum -y install mariadb mariadb-server 给root用户设置密码 >>>mysqladmin -uroot ...

  8. 20180426 Linq to excel

    PM> Install-Package LinqToExcel 引用好像可以直接把excel 导入得到实体对象,  var excelfile = new ExcelQueryFactory(& ...

  9. Scala对class/object反射

    近期有需求,要根据解析字符串,根据字符串的内容去调用方法.想到的基本就是使用反射.但是基本上能找到的资料,全没有讲scala的反射.只有零星点点的讲解.大部分都是用scala的语法写java反射最后翻 ...

  10. [vue]声明式导航和编程式导航

    声明式导航和编程式导航 共同点: 都能进行导航,都可以触发路由,实现组件切换 区别: 写法不一样,声明式导航是写在组件的template中,通过router-link来触发,编程式导航写在js函数中, ...