1:定时器
····
/*
setTimeout 设置过期时间
setTimeout(时间到了之后要执行的行为,什么时间 毫秒 开始执行)
setInterval 设置中断时间
setInterval(时间到了之后要执行的行为 , 间隔多长时间再执行 )
*/
// 两秒之后执行,执行完当前函数,不再重复执行
setTimeout(function(){
console.log("爆炸了");
} , 2000);
// 只是中断,每个一段时间休息一回,然后接着执行
setInterval(function(){
console.log("死了一回");
} , 2000 );
function daojishi(){
let t = new Date();
document.getElementById("showTime").innerHTML = t.toLocaleTimeString();
}
let fafuqushi = setInterval( daojishi , 1000 );
function stop(){
clearInterval(fafuqushi);
}
你敢按吗?
····
2:一闪一闪亮晶晶
····
html>
#container{
width: 400px;
height: 400px;
border: 1px solid yellowgreen;
background-color: black;
position: relative;
}
span{
font-size: 30px;
color: yellow;
position: absolute;
}
var timer;
function start_flash(){
timer = setInterval(function(){
document.getElementById("container").innerHTML = " * ";
let x = parseInt( Math.random()*400 + 1 );
let y = parseInt( Math.random()*400 + 1 );
document.getElementById("container").firstElementChild.style.left = x + "px";
document.getElementById("container").firstElementChild.style.top = y + "px";
} , 200 );
}
function stop_flash(){
clearInterval(timer);
}
亮晶晶
停止亮晶晶