老师,一个关于代码执行的问题
来源:1-21 编程练习
TaraTara
2020-04-21 16:30:51
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>定时器</title>
<style type="text/css">
div{width:400px;height:120px;margin-top:50px;border:2px solid gray;padding:10px;}
</style>
</head>
<body>
<input type="button" value="删除">
<input type="button" value="取消删除">
<div>点击"删除"按钮后,里面的内容将在3秒钟后消失;<br/><br/>如点击了"删除"后又不想删除内容,请在点击"删除"按钮3秒之内点击"取消删除"按钮即可</div>
<script type="text/javascript">
var word=document.getElementsByTagName("div")[0],
del=document.getElementsByTagName("input")[0],
undel=document.getElementsByTagName("input")[1];
del.onclick=function(){
time=window.setTimeout(function(){word.innerHTML=""},3000);
}
undel.onclick=function(){
window.clearTimeout(time);
}
</script>
</body>
</html>请问老师,下面这一行的含义难道不是赋值吗(等号右边的值赋值给time)?为什么同时会执行window.setTimeout?
time=window.setTimeout(function(){word.innerHTML=""},3000);1回答
同学你好,关于同学的问题回答如下:
1、代码实现的是正确的。
2、这句代码是把定时器赋值给变量time:

把右侧的结果赋值给time,便于我们清除定时器。跟普通的函数赋值是一样的。
祝学习愉快~
相似问题