网站首页 > 知识剖析 正文
关于定时器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>
猜你喜欢
- 2024-11-11 windows时间同步服务器设置 win10时间同步服务器设置
- 2024-11-11 javaScript 使用定时器 js实现定时器功能
- 2024-11-11 关于JavaScript中this关键字指向的问题
- 2024-11-11 如何理解JavaScript定时器的4种写法「带面试题讲解」
- 2024-11-11 如何设置Windows系统 Internet时间的同步周期
- 2024-11-11 setInterval、setTimeout和requestAnimationFrame
- 2024-11-11 手把手教你制作智能桌宠(小可爱哦!)
- 2024-11-11 setInterval()和setTimeout()区别及清除定时器
- 2024-11-11 第37节 计时器setTimeout和setInterval
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)