您的位置:首页 > Web前端 > JavaScript

JavaScript定时器和进度条案例

2016-03-13 19:02 645 查看
Javascript中的定时器

window对象下面的方法 window.setInterval() interval 间隔 setInterval 建立时间间隔

1.setInterval("function",time) 重复执行某项操作。

时间单位是毫秒

2.setTimeout 运用在延迟一段时间,再进行某项操作。

setTimeout("function",time),setTimeout不会重复

clearTimeout(对象) 清除已设置的setTimeout对象

clearInterval(对象) 清除已设置的setInterval对象

<script language="JavaScript" type="text/javascript">

<!--

function hello(){

alert("hello");

}

window.setTimeout(hello,5000);

//-->

window.setTimeout("hello()",5000);

btnReset.onclick=function(){

seed=0;

}

style.width,offsetWidth

style.width 后面加px 而offsetwidth不加px

console.log() // 控制台日志输出!!!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
#a1
{
margin-top:100px;
margin-left:30px;
background-color:white;
border:solid black 1px;
width:1004px;
height:54px;
}
#a2
{
margin-top:2px;
margin-left:2px;
background-color:red;
width:0px;
height:50px;

}
</style>
<script type="text/javascript" src="2.js"></script>
</head>

<body onload="start();">
<div id="a1">
<div id="a2">
</div>
</div>
</body>
</html>


// JavaScript Document
function show()
{
if(document.getElementById("a2").offsetWidth<1000)
{
document.getElementById("a2").style.width=
document.getElementById("a2").offsetWidth+30+"px";
console.log(document.getElementById("a2").offsetWidth);
}
else
{
document.getElementById("a2").style.width=1000+"px";
stop();
console.log(document.getElementById("a2").offsetWidth);
}
}
var time;
function start()
{
time=window.setInterval("show()",50);
}
function stop()
{
clearInterval(time);
}










内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: