console.log(Date.now());
//定时1秒后执行
var timeout=setTimeout(changebackgroundcolor,1000);
//循环执行
var timeout=setInterval(changebackgroundcolor,1000);
具体项目
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
#box1{
width: 100px;
height: 100px;
background-color: red;
}
</style>
<body>
<div id="box1"></div>
</body>
<script>
var flag=true;
function changebackgroundcolor(){
var box=document.getElementById("box1");
if(flag){
box.style.backgroundColor="black";
flag=false;
console.log(Date.now());
}
else{
box.style.backgroundColor="#bfa";
flag=true;
console.log(Date.now());
}
}
console.log(Date.now());
//定时1秒后执行
// var timeout=setTimeout(changebackgroundcolor,1000);
//循环执行
var timeout=setInterval(changebackgroundcolor,1000);
</script>
</html>