定时器:在屏幕打印五秒,到五秒时弹出停止的会话框;
一般用超时调用的这种方法:
var num=0;
var max=5;
function box(){
num++;
if(num==max){
alert("5s is over!!");
}
else(
setTimeout(box,1000);
)
}
setTimeout(box,1000);
但也可以用这种方式实现:
var num=0;
var max=5;
var id=null;
function box(){
num++;
document.getElementById('a').innerHTML +=num;
if(num==max){
clearInterval(id);
alert("5s is OVAR!!!");
}
}
id=setInterval(box,1000);
HTML页面代码很简单,具体如下:
<div id="a"></div>
跨浏览器获取可视化页面窗口
var width = window.innerWidth;
var height = window.innerHeight;
if(typeof width!='number'){
if(document.compatMode=='CSS1Compat'){
width=document.documentElement.clientWidth;
height=document.documentElement.clientHeight;
}
else{
width=document.body.clientWidth;
height=document.body.clientHeight;
}
}
alert(width); //widht\height各浏览器 的值各不相同
alert(height);
本文介绍了一个使用JavaScript实现的定时器示例,通过两种不同的方法来显示倒计时,并在达到设定时间后弹出提示框。此外,还提供了一种跨浏览器获取可视化页面窗口尺寸的方法。

被折叠的 条评论
为什么被折叠?



