博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JS中SetTimeOut和SetInterval方法的区别?
阅读量:4677 次
发布时间:2019-06-09

本文共 1003 字,大约阅读时间需要 3 分钟。

1.setInterval() 方法可按照指定的周期(以计)来调用函数或计算表达式。

setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<html>
<body>
 
<input type=
"text" 
id=
"clock" 
size=
"35" 
/>
<script language=javascript>
var 
int=self.setInterval(
"clock()"
,50)
function 
clock()
  
{
  
var 
t=
new 
Date()
  
document.getElementById(
"clock"
).value=t
  
}
</script>
</form>
<button onclick=
"int=window.clearInterval(int)"
>
Stop interval</button>
 
</body>
</html>

2.setTimeout() 方法用于在指定的数后调用函数或计算表达式。setTimeout() 只执行 code 一次。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<html>
<head>
<script type=
"text/javascript"
>
function 
timedMsg()
{
   
var 
t=setTimeout(
"alert('5 seconds!')"
,5000)
}
</script>
</head>
 
<body>
<form>
<input type=
"button" 
value=
"Display timed alertbox!" 
onClick=
"timedMsg()"
>
</form>
<p>Click on the button above. An alert box will be displayed after 5 seconds.</p>
</body>
 
</html>

转载于:https://www.cnblogs.com/luxj/p/6132754.html

你可能感兴趣的文章
Python(简单图形和文件处理)编程
查看>>
C#写点酷玩意,波形图控件
查看>>
MS SQL 批量操作
查看>>
CSRF
查看>>
mysql中创建用户和赋权限
查看>>
(Hive)史上最难解析的json字符串解析出来了!!
查看>>
Linux学习之一--VI编辑器的基本使用
查看>>
Activity启动模式 及 Intent Flags 与 栈 的关联分析
查看>>
iOS5可能会删除本地文件储存
查看>>
周四总结
查看>>
播放器
查看>>
linux使用select实现精确定时器详解
查看>>
隐藏 iframe 技术——Ajax 时代一个重要的环节
查看>>
腾讯通启动报错please intall msxml5.0 or later!
查看>>
JQuery攻略(一) 基础知识——选择器 与 DOM
查看>>
字符串hash
查看>>
2017《面向对象程序设计》课程作业六
查看>>
404 Note Found 队-Beta6
查看>>
[转载]实际举例C#引用类型和值类型的区别
查看>>
狼人杀校园升级版:学霸大战学渣 Who is the king of examination!
查看>>