模拟setTimeOut

Posted on 2019-04-10 09:35  勤勤恳恳大猿人  阅读(172)  评论(0编辑  收藏  举报

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
function getTime() {
return new Date().getTime()
}

function test(){
let perTime = getTime();
let timer = null;
let i=0;
timer = requestAnimationFrame(run);

function run(){
let nowTime = getTime();
if(nowTime - perTime >5000){
perTime = getTime();
console.log(i++)
}
timer = requestAnimationFrame(run);
}
}

test();
</script>
</body>
</html>