当前位置: 首页 > article >正文

【JavaScript】一文了解定时器的使用

🍈作者简介:大家好,我是亦世凡华、渴望知识储备自己的一名在校大学生

🍇个人主页:亦世凡华、的博客

🍓系列专栏:JavaScript专栏

🥝推荐一款模拟面试刷题神器🔥:点击跳转进入网站

目录

🏍️定时器

🍇setTimeout() 超时定时器

🍈clearTimeout() 清除超时定时器

🍉setInterval() 间隔定时器

🍊clearInterval() 清除间隔定时器


🏍️定时器

window对象给我们提供了两种定时器

🍇setTimeout() 超时定时器

setTimeout()方法用于设置一个定时器,该定时器在定时器到期后执行

window.setTimeout(调用函数,[延迟的毫秒数]);
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script>// setTimeoutsetTimeout(function(){// console.log('看我执行的时间');},2000) //延迟单位是毫秒数function time(){console.log('我执行了多久');}var timer = setTimeout(time,2000)// setTimeout('time()',2000) 不提倡这种写法</script>
</body>
</html>

总结

1.window可以省略

2.调用函数可以直接写函数或函数名或采用字符串 '函数名()'三种形式。第三章不推荐

3.延迟的毫秒数默认是0,如果写必须是毫秒

4.定时器如果有很多的话需要给定时器赋值一个标识符

setTimeout()这个调用函数也称为回调函数,普通函数是按照代码顺序直接调用,而这个函数需要等待时间,时间到了才会去调用这个函数,因此称为回调函数。简单理解:回调就是回头调用的意思,上一件事干完再回头调用这个函数。

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><img src="./img1.jpg"><script>var img = document.querySelector('img');setTimeout(function(){img.style.display = 'none';},5000)</script>
</body>
</html>

🍈clearTimeout() 清除超时定时器

clearTimeout()取消了先前调用setTimeout()建立的定时器

window.clearTime(timeout ID)
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><button>阻止按钮</button><script>var time = setTimeout(function(){console.log('快阻止我吧');},4000)var btn = document.querySelector('button');btn.addEventListener('click',function(){clearTimeout(time);})</script>
</body>
</html>

注意

1.window可以省略

2.里面的参数就是定时器的标识符

🍉setInterval() 间隔定时器

setInterval()方法反复调用一个函数,每隔这个时间就去调用一次回调函数。

window.setInterval(回调函数,[间隔的毫秒数])
<script>setInterval(function(){console.log('我会间隔执行哦');},2000)
</script>

倒计时案例

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>span{background-color: #000;display: block;float: left;margin-right: 10px;margin-top: 300px;width: 40px;height: 40px;color: #fff;text-align: center;line-height: 40px;}.hour{margin-left: 45%;}</style>
</head>
<body><div><span class="hour">1</span><span class="mintue">2</span><span class="second">3</span></div><script>// 1.获取元素var hour = document.querySelector('.hour');var mintue = document.querySelector('.mintue');var second = document.querySelector('.second');var inputTime = +new Date('2022-7-27 18:00:00'); // 返回的是用户输入时间的总的毫秒数// 第一次执行也是间隔毫秒数,因此刚刷新页面会有空白countDown();//我们先调用这个函数,防止第一次刷新页面有空白// 2.开启定时器setInterval(countDown,1000)function countDown () {var nowTime = +new Date(); // 返回的是当前时间总的毫秒数var times = (inputTime - nowTime) / 1000; // time是剩余时间总的秒数var h = parseInt(times / 60 / 60 % 24); //计算小时h = h < 10 ? '0' + h : h;hour.innerHTML = h;//把剩余的小时给 黑色的小时盒子var m = parseInt(times / 60 % 60); //计算分数m = m < 10 ? '0' + m : m;mintue.innerHTML = m;var s = parseInt(times % 60); //计算当前秒数s = s < 10 ? '0' + s : s;second.innerHTML = s;}</script>
</body>
</html>

🍊clearInterval() 清除间隔定时器

clearInterval()方法取消了先前调用setInterval()建立的定时器。

window.clearInterval(interval ID)
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><button class="begin">开启定时器</button><button class="stop">关闭定时器</button><script>var begin = document.querySelector('.begin');var stop = document.querySelector('.stop');var timer = null;//全局变量begin.addEventListener('click',function(){timer = setInterval(function(){console.log('我在执行哦');},1000)})stop.addEventListener('click',function(){clearInterval(timer)})</script>
</body>
</html>

总结

setTimeout和setInterval的区别:

setTimeout:延时时间到了就去执行回调函数,只调用一次就结束这个定时器

setInterval:每隔这个延时时间就去调用回调函数,会重复调用这个回调函数

🍃JavaScript的学习还是要以多练习为主,想要练习JavaScript的朋友,推荐可以去牛客网看一看,链接:牛客网 里面的IT题库内容很丰富,属于国内做的很好的了,最重要的是里面的资源是免费的,是课程+刷题+面经+求职+讨论区分享,一站式求职学习网站,感兴趣的可以去看看。

呜呜~,原创不易。

http://www.lryc.cn/news/2419450.html

相关文章:

  • Windows7系统explorer.exe文件问题
  • 约瑟夫环问题(队列,链表实现)- c++
  • 系统编程之文件IO(四)——初级IO(open、close、write、lseek)
  • JS中clientWidth offsetWidth innerWidth scrollWidth等区分
  • 经纬度有哪些格式
  • WAV文件格式详解
  • Ubuntu (安装问题,包括系统更新和软件安装)
  • 软件工程与计算II-12-详细设计
  • i386和X86各是什么意思 与arm的区别
  • 人脸对齐 matlab,常用几种人脸对齐算法ASM/AAM/CLM/SDM
  • 计算机网络知识之URL、IP、子网掩码、端口号
  • 磁力链接转换为种子文件 magnet to torrent
  • 深入浅出声学系统频率响应
  • Android开发者必须收藏的8个开源库,值得收藏!_android 开源鉴黄
  • 关于System.currentTimeMillis()的理解
  • python的np.meshgrid函数
  • 数字后端概念——shielding
  • 用hist()绘制直方图
  • [转]推荐一款新型 Java 网站内容管理系统,灵活、易用,运行稳定,轻松管理建设网站(附源码)
  • Linux tar命令详解,Linux备份解压文件_linux tar备份文件
  • 新手怎么炒外汇?
  • 【合唱】男女差八度的科学解释
  • handoop job工作运行的机制与原理详解
  • 20款最流行的免费定性数据分析工具
  • 主数据管理和实施
  • Linux 详解:最完整的入门指南_linux菜鸟入门指南
  • 【游戏】如何开发一款游戏:游戏开发流程及所需工具
  • 飞鸡:从小训练飞行的鸡能飞行吗?为什么野鸡能飞吗?是同一品种吗?今天自由思考
  • c++_ifstream,ofstream读写文件
  • 使用rkhunter检测Linux的rootkit