<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>间隔打印</title>
</head>
<body>
定时遍历数组值,间隔打印 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
<div id="text"></div>
</body>
<script type="text/javascript">
let dataList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let sleep = 5000; //时间 5s
function runEachArray(sleep, arr, index) {
if (index == null) {
index = 0
}
//div 赋值
document.getElementById('text').innerHTML = arr[index];
index++;
if (index <= arr.length) {
setTimeout(() => {
runEachArray(sleep, arr, index);
}, sleep)
} else {
runEachArray(sleep, arr, null)
}
}
runEachArray(sleep, dataList, null)
</script>
</html>
JavaScript 间隔5s 依次打印数组中的值,无限循环执行
最新推荐文章于 2023-06-13 15:01:21 发布