@[TOC](JS自定义延迟函数sleep())
首先测试网上看到的循环空代码的方式
单步延迟
主要代码:
function sleep(delay) {
let endTime = new Date().getTime()+parseInt(delay);
while (new Date().getTime() < endTime );
}
全部代码:
<!DOCTYPE html>
<html lang="cn">
<head>
<meta charset="UTF-8">
<title>测试sleep</title>
</head>
<script>
function Test(){
console.log("开始运行");
sleep(20000);//等待20秒
console.log("运行结束");
}
function sleep(delay) {
let endTime = new Date().getTime()+parseInt(delay);
while (new Date().getTime() < endTime );
}
</script>
<body>
<input type="button" value="Test" onclick="Test()">
</body>
</html>
结果:
循环延迟:
主要代码:
function sleep(delay) {
let endTime = new Date().getTime()+parseInt(delay);
while (new Date()<