1、其他函数
obj = setInterval('函数名称', 毫秒数); clearInterval(obj); --->这就相当于一个定时器;
obj = setTimeout('函数名称', 毫秒数); clearTimeout(obj);
2、滚动条
!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>页面一</title>
</head>
<body>
<hr/>
<div style = 'width:500px;background-color:#ddd'>
<div id = 'process' style = 'width:10%;height:10px;background-color:green;'></div>
</div>
<script type = 'text/javascript'>
/* window.prb = 10;
function Foo(){
var id = document.getElementById('process');
window.prb += 10;
if(window.prb > 100){
clearInterval(window.interval);
}else{
id.style.width = window.prb + '%';
}
}
window.interval = setInterval('Foo()', 500); //设置定时器
*/
window.prb = 10;
function Foo(){
var id = document.getElementById('process');
window.prb += 10;
if(window.prb > 100){
clearTimeout(window.interval);
}else{
id.style.width = window.prb + '%';
}
}
window.interval = setTimeout('Foo()', 500);
</script>
</body>
</html>
运行结果
3、×××灯
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>欢迎上级领导来校视察 </title>
</head>
<body>
<input type = 'button' value = '关掉' onclick = 'stop();'/>
<script type = 'text/javascript'>
function Go(){
var content = document.title; //DOM获得标签的内容
var firstChar = content.charAt(0);
var sub = content.substring(1, content.length);
document.title = sub + firstChar;
}
interval = setInterval('Go()', 200);
function stop(){
clearTimeout(interval); //停止当前的程序运行。
}
</script>
</body>
</html>
运行结果
4、总结
(1)、num = setInterval('事件函数', 毫秒),这个函数(setInterval)就是一个定时器,每隔多长时间执行一次这个函数;
clearInterval(num),就是终止执行定时器函数;
(2)、num = setTimeout('事件函数', 毫秒),这个函数只执行一次,就不在执行;
clearTimeout(num),终止定时器的执行;
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。