var ret;// 全局变量functionstart(){
ret =setInterval(function(){var now =newDate();
console.log(now.toLocaleString())},2000)}functionstop(){clearInterval(ret);}<button onclick="start()">开始定时器</button><button onclick="stop()">结束定时器</button>
var timer;functiontimeoutStart(){if(confirm("确认打印输出吗?")){alert("请等待5秒");
timer =setTimeout(function(){
document.write("hello world")},5000)}else{alert("取消")}}functiontimeoutStop(){clearTimeout(timer)}<button onclick="timeoutStart()">开始定时器</button><button onclick="timeoutStop()">结束定时器</button>
4、window中的属性
1、screen:获取客户端显示器相关信息
属性
说明
width
宽
height
高
availWidth
可用宽
availHeight
可用高
functionscreenInfo(){var w = screen.width;var h = screen.height;
console.log("宽度:"+ w +",高度:"+ h);// 宽度:1366,高度:768var aw = screen.availWidth;var ah = screen.availHeight;
console.log("可用宽度:"+ aw +",可用高度:"+ ah);// 可用宽度:1304,可用高度:768}<button onclick="screenInfo()">屏幕信息</button>