JavaScript 定时器和延时器(js定时器的原理)

boyanx5个月前技术教程17

关于定时器setInterval(code, millisecond)和延时器setTimeout(code, millisecond)中第一个参数引号问题思考

对于自定义函数使用双引号必须加上括号;

setInterval("start()", 1000);
setTimeout("start()", 1000);

可以简化为

setInterval(start, 1000);
setTimeout(start, 1000);

start 为自定义函数的名称

停止定时器和延时器的函数

clearInterval()
clearTimeout()

对于执行语句中变量必须使用字符串连接符"+", 并且外引号对应;

执行语句使用双引号:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload = function(){
    var i=10;
    timer = setTimeout("alert('骨干分"+i+"子')",3000); //注意引号和变量的使用方式
}
function stop(){
    clearTimeout(timer);
}
</script>
</head>
<body>
<button onclick="stop()">停止</button>
</body>
</html>

执行语句使用单引号

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload = function(){
var i=10;
timer = setTimeout('alert("我是第'+i+'个人")',3000); //注意引号和变量的使用方式
}
function stop(){
clearTimeout(timer);
}
</script>
</head>
<body>
<button onclick="stop()">停止</button>
</body>
</html>

直接使用匿名函数:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload = function(){
var i=10;
timer = setTimeout(function(){
alert("我是第"+i+"个人");
},3000); //注意引号和变量的使用方式
}
function stop(){
clearTimeout(timer);
}
</script>
</head>
<body>
<button onclick="stop()">停止</button>
</body>
</html>

以上三个案例:只弹出一次警告框"我是第10个人"

实例1:简单计时器

第一种方法:使用延时器

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>网页标题</title>
<script type="text/javascript">
//实现简单的计时器
var i = 0;
var timer;
function start(){
i++;
//第一步:要获取到id=res这个对象
var btnObj = document.getElementById("res");
btnObj.innerHTML = "程序运行了<font color='red'>"+i+"</font>秒";
//使用延时器 每隔1秒后调用这个函数
timer = setTimeout("start()",1000); //使用递归函数
}
//所谓的停止 就是用来清除延时器
function stop(){
clearTimeout(timer);
}
</script>
</head>
<body>
<button id="res">程序运行了0秒</button><br/>
<button onclick="start()">开始</button>
<button onclick="stop()">停止</button>
</body>
</html>

第二种方法:使用定时器

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
var i=0
function start(){
var oBtn = document.getElementById("btn");
timer= setInterval(function(){ //使用匿名函数
oBtn.innerHTML = "程序运行了"+i+"秒";
i++;
}, 1000);
}
function stop(){
clearInterval(timer);
}
</script>
</head>
<body>
<button id="btn">程序运行了0秒</button><br />
<button onclick="start()">开始</button>
<button onclick="stop()">停止</button>
</body>
</html>

做简单计时器两种方法的比较:

延时器要使用递归函数, 定时器不必使用递归函数

实例2: 文字滚动:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<script type="text/javascript">
//当页面加载完成后
window.onload = function(){
window.setInterval("start()",50);
}
var str = "武汉传智PHP1期基础班";
var str_leng = str.length; //这个字符串初始的长度
var flag = "right"; //人为定义一个方向 来告诉向右走
//这个函数的功能主要是用于实现文字滚动
//1.需要先获取id=input这个对象 然后给这个对象的value前面加空格
function start(){
var inputObj = document.getElementById('input');
if(flag == "right"){
//向右
//获取id=input这个对象
str = " "+str;
//再kongge这个字符串赋值给inputObj这个对象的value
inputObj.value = str;
if(str.length == 55){
flag = "left";
}
}else{
//向左
//每隔50毫秒删除一个空格
str = str.substr(1);
inputObj.value = str;
if(str.length == str_leng){
flag = "right";
}
}
}
</script>
</head>
<body>
<input id='input' size="40" />
</body>
</html>

实例3: 图片轮播

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
.box{
width:500px;
height:160px;
margin:50px auto;
}
</style>
<script type="text/javascript">
window.onload = function(){
setInterval("start()",500);
}
var i=1;
function start(){
var oImg = document.getElementById("img");
i++;
oImg.src = "image/dd_scroll_"+i+".jpg";
if(i==6){
i=0;
}
}
</script>
</head>
<body>
<div class="box">
<img src="image/dd_scroll_1.jpg" id="img"/>
</div>
</body>
</html>


实例4: 倒计时

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
button{
width: 100px;
}
</style>
</head>
<body>
<button>发送验证码</button>
<script type="text/javascript">
var btn = document.getElementsByTagName("button")[0];

var num = 10;
var timer = null;
btn.onclick = function(){
btn.disabled = "disabled";
clearInterval(timer);

timer = setInterval(function(){
if(num===-1){
clearInterval(timer);//如果num=0我就让定时器停下来

btn.removeAttribute("disabled");
num = 10;
btn.innerText = "发送验证码";
}else{
btn.innerText = num+"s";
num--;
}


},1000)



}


</script>
</body>
</html>

实例5:显示动态时间

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>动态显示时间</title>

<style type="text/css">
#times{
width: 200px;
height: 20px;
border: 3px solid gray; /*如果不加实线无法显示边框*/
}
</style>
</head>

<body>
<div id="times">

</div>

<script type="text/javascript">
//得到时间并写入div
function getDate(){
//获取当前时间
var date = new Date();
//格式化为本地时间格式
var date1 = date.toLocaleString();
//获取div
var div1 = document.getElementById("times");
//将时间写入div
div1.innerHTML = date1;
}
//使用定时器每秒向div写入当前时间
setInterval("getDate()",1000);
</script>
</body>
</html>

实例6: 随机点名

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
div{
width: 200px;
height: 60px;
font-size: 36px;
text-align: center;
line-height: 60px;
border: 1px solid #000;
margin: 100px auto;
}
</style>
</head>
<body>
<button id="start">开始</button>
<button id="end">结束</button>
<div id="div">梁永灿</div>

<script type="text/javascript">
//准备人名
var arr = ["张三","李四","王五","老六"];


var timer = null;
start.onclick = function(){
//以防沙雕用户频繁点击 我每次点击都把上一次的定时器清空
clearInterval(timer);

timer = setInterval(function(){
div.innerText = arr[Math.floor(Math.random()*arr.length)];
},10)
}


end.onclick = function(){
clearInterval(timer);
}
</script>
</body>
</html>
标签: js日期

相关文章

JavaScript:history和location对象、JS设计模式系统讲解与应用

一、history对象history 对象是历史对象。包含用户(在浏览器窗口中)访问过的 URL。history 对象是 window 对象的一部分,可通过 window.history 属性对其进行...

减少复杂性!JavaScript 2024 发展趋势预判

【CSDN 编者按】这是一篇由 Ryan Carniato 于2023年12月29日发表的关于「2024年JavaScript 框架发展趋势」的文章。作者认为 2023 年是 JavaScript 框...

什么是 JavaScript?(什么是生理性喜欢)

本文首发自「慕课网」,想了解更多IT干货内容,程序员圈内热闻,欢迎关注!作者|慕课网精英讲师 然冬JavaScript ( JS ) 是一种具有函数优先的轻量级,解释型或即时编译型的编程语言。(MDN...

js计算两个时间相差(js计算两个时间的时间差)

DateDifference (faultDate, completeTime) { var stime = new Date(faultDate).getTime() var etime = new...

JavaScript需要掌握的技能盘点(JS入门需看)

JavaScript是当今使用的最重要的 Web 开发语言之一。它使您可以为您的网站添加广泛的功能特性,从最基本得到最高级的。因此,无论您是专家级开发人员还是刚起步的初学者,您都需要了解某些关键的Ja...

Firefox火狐浏览器总结2023年:网页加载、JS执行速度均有提升

IT之家 11 月 27 日消息,Mozilla 日前发布官方新闻稿,盘点了 FireFox 火狐浏览器在 2023 年取得的进展,主要围绕速度方面做出了三大改进,分别是网页加载速度、键盘响应速度、J...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。