gpt4 book ai didi

javascript - 打印数字 - setTimeout JavaScript

转载 作者:行者123 更新时间:2023-12-03 00:01:31 25 4
gpt4 key购买 nike

我正在尝试将数字从 0 -5 打印到我的 div(滚动),然后暂停 2 秒,然后从 5-0 打印。怎么了?

var i = 0;
var j = 5;
var scroll = document.getElementById('scroll');

function start() {
scroll.textContent += i;
setTimeout(start, 2000)
i++;
if (i > 5) {
setTimeout(start2, 2000)
scroll.textContent = '';
}
}

function start2() {
scroll.textContent += j;
setTimeout(start2, 2000)
j--;
if (j == 0) {
setTimeout(start, 200)
}

}

start();
<div id="scroll"></div>

最佳答案

var j = 5;
var scroll = document.getElementById('scroll');

function start() {
for (let i = 0; i <= j; ++i)
scroll.textContent += i;

setTimeout(start2, 2000)
}

function start2() {
for (let i = j; i; --i)
scroll.textContent += i;

setTimeout(start, 2000) // should you?
}

start();
<div id="scroll"></div>

关于javascript - 打印数字 - setTimeout JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55150191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com